# delimit;
set more off;
clear all;
set maxvar 10000;
cap log close;

global maindir "~/projects/tornadoes";

log using ${maindir}/logs/analyze_tornadoes_20220330.txt, text replace;


* Switching back to regular clustered standard errors since bootstrapped ones were the same;


* Swithces for paper;
local makemeanfigures = 1;
local combinebusandccpfigs = 1;

* OLS Event Studies;
local makefigure3 = 1;
local makemigfigure3 = 1;

local makefigure5 = 1;
local makemigfigure5 = 1;

local olseventstudyplots2x4 = 1;


local maketable1 = 1;
local maketable3 = 1;
local maketable4 = 1;


* Switches for appendix;
local makeapptable3 = 1;
local makeapptable4 = 1;
local makeapptable6 = 1;
local makeapptable7 = 1;
local makeapptable8 = 1;
local makeapptable11 = 1;
local makeapptable12 = 1;
local makeapptable13 = 1;
local makeapptable14 = 1;
local makeapptable15 = 1;

* Appendix Figure 14;
local makeannualddbjseventstudy = 1;
local makeannualddbjsmigeventstudy = 1;
local bjsplots2x4 = 1;










* Control Sample: 0.5-1.5 miles and 1-2 miles from tornado boundary;
local samples = "near_05_15 near_1_2";
* Tornado samples: Shawn is 34 tornadoes, zscoreccp8 is balanced 9 cash/9 no cash, full is all 35 tornadoes;
local tsamples = "shawn zscoreccp8 full";

local figure3vars = "bal_bcret_tot bal_home_tot_cond bal_auto_tot bal_other2_tot riskscore gt1_all_90";
local figure3migvars = "moveblock moveblock3yr";
local figure5vars = "bal_bcret_tot bal_home_tot_cond bal_auto_tot bal_other2_tot riskscore gt1_all_90";
local figure5migvars = "moveblock moveblock3yr";



* Tables for JAERE Final;
local table1vars = "bal_bcret_tot bal_home_tot_cond bal_auto_tot bal_other2_tot riskscore gt1_all_90";
local table3vars = "bal_bcret_tot bal_home_tot_cond bal_auto_tot bal_other2_tot riskscore gt1_all_90";
local table4vars = "bal_bcret_tot bal_home_tot_cond bal_auto_tot gt1_all_90";

local bjsvars = "bal_bcret_tot bal_home_tot_cond bal_auto_tot bal_other2_tot riskscore gt1_all_90";

local apptable3varspanela = "bal_bcret_tot bal_home_tot bal_auto_tot bal_other2_tot bal_newtotal_tot riskscore gt1_all_90 fstart_cond";
local apptable3varspanelb = "fooblock faablock fhispblock f65pblock";
local apptable6vars = "bal_bcret_tot bal_home_tot_cond bal_auto_tot bal_other2_tot";
local apptable7vars = "riskscore gt1_all_90";
local apptable8vars = "bal_home_tot_cond_mat bal_home_tot_cond_sat bal_fmrtg_tot_cond bal_hel_tot_cond";
local apptable11vars = "numautoopen balnewauto";
local apptable12vars = "numautoopen balnewauto";

* Migration Sample Variables;
local apptablemigddvars = "moveblock moveblock3yr";

local tablemigddvars = "moveblock moveblock3yr";
local tablemigdddvars = "moveblock moveblock3yr";

local table1migvars = "moveblock moveblock3yr";
local table3migvars = "moveblock moveblock3yr";
local table4migvars = "moveblock moveblock3yr";


local bjsmigvars = "moveblock moveblock3yr";

* End of Tables for JAERE R&R;


*  we are using 50%(tt=1);
*  in damage path or control band requirement;



program define openregulardataset;
* takes in sample and tsample as arguments 1 and 2;

disp("Tornado sample: `2'");                               

use ${maindir}/tmp_data/ccp_tornado_hetall_ttype1_bal12prepost_for_analysis_`1'.dta, clear;

 
if ("`2'" == "shawn") {;
  * Shawn's sample;
  drop if (id == 111);                      
};
else if ("`2'" == "full") {;
  * full sample;
  drop if (id==0);                      
};
else if ("`2'" == "zscoreccp8") {;
  * Z-score CCP 8 var sample;
  drop if (id == 111 | (ia == 1 & (id == 3 | id == 18 | id == 24 | id == 25 | id == 26 | id == 28 | id == 45 | id == 52 | id == 55 | id  == 70 | id == 91 | id == 102  | id == 104 | id == 107 | id == 108 | id == 114 | id == 111)));                      
};

    
egen ncid = group(cid);
xtset ncid;

sort ncid id qdate;
count if ncid==ncid[_n-1] & id~=id[_n-1];
replace id = id[_n-1] if ncid==ncid[_n-1] & id~=id[_n-1];

end;





program define openmigrationdataset;
* takes in sample and tsample as arguments 1 and 2;


disp("Tornado sample Migration analysis: `2'");                               
use ${maindir}/tmp_data/ccp_tornado_sample_not_balanced_within_hetall_`1'_migration.dta, clear;


if ("`2'" == "shawn") {;
  * Shawn's sample;
  drop if (id == 111);                      
};
else if ("`2'" == "full") {;
  * full sample;
  drop if (id==0);                      
};
else if ("`2'" == "zscoreccp8") {;
  * Z-score CCP 8 var sample;
  drop if (id == 111 | (ia == 1 & (id == 3 | id == 18 | id == 24 | id == 25 | id == 26 | id == 28 | id == 45 | id == 52 | id == 55 | id  == 70 | id == 91 | id == 102  | id == 104 | id == 107 | id == 108 | id == 114 | id == 111)));                      
};

egen ncid = group(cid);
xtset ncid;

sort ncid id qdate;
count if ncid==ncid[_n-1] & id~=id[_n-1];
replace id = id[_n-1] if ncid==ncid[_n-1] & id~=id[_n-1];

end;






* main CCP Results and then Migration Results;


local sample = "near_05_15";
local tsample = "shawn";

disp("Sample: `sample'");
disp("T-Sample: `tsample'");
 

 

* beginning of making Credit Card Mean Figure;
if(`makemeanfigures'==1) 
{;
*Credit Card Mean Figure;

openregulardataset `sample' `tsample';

* Stat about non credit constrained moving;
sum moveoutblock if num_home_tot ==0 & qsince==4 & num_home_tot[_n-5] > 0 & hrs==1 & treated ==1 & ia==1;

* Residualize auto trade variable for figure;
reg balnewauto i.qdate;
predict r_balnewauto, resid;                               
reg lbalnewauto i.qdate;
predict r_lbalnewauto, resid;                               
reg numautoopen i.qdate;                              
predict r_numautoopen, resid;



tab id;
collapse (sum) obs (mean) r_balnewauto r_lbalnewauto r_numautoopen balnewauto bal_bankc_tot fstart_cond, by(qsince ia treated) fast;
keep r_balnewauto r_lbalnewauto r_numautoopen balnewauto bal_bankc_tot fstart_cond obs qsince ia treated;
reshape wide r_balnewauto r_lbalnewauto r_numautoopen balnewauto bal_bankc_tot fstart_cond obs, i(qsince ia) j(treated);

reshape wide obsnum0-fstart_cond1, i(qsince) j(ia);

local var "bal_bankc_tot";
  // more narrow credit card def;
*local var "bal_bcret_tot";
  // broader credit card def;
  
  
foreach v of local var {;
preserve;
keep `v'*  qsince;
label var `v'01 "Nearby-Aid";
label var `v'11 "Hit-AId";
label var `v'00 "Nearby-No Aid";
label var `v'10 "Hit-No Aid";

gen `v'_ia_d = `v'11-`v'01;
gen `v'_nia_d = `v'10-`v'00;

label var `v'_ia_d "Individual Assistance(IA)";
label var `v'_nia_d "No IA";


twoway (connected `v'00 qsince, lcolor(blue) msymbol(triangle_hollow) mcolor(blue) lpattern(dash)) 
    (connected `v'01 qsince, lcolor(red) msymbol(circle_hollow) mcolor(red)) 
	(connected `v'10 qsince, lcolor(dkgreen) msymbol(triangle) mcolor(dkgreen) lpattern(dash) )
    (connected `v'11 qsince, lcolor(dkorange) msymbol(circle) mcolor(dkorange)), 
	ytitle("{bf:Credit Card Debt}") xline(-1, lcolor(black)) ytitle(, margin(small)) xtitle("{bf:Quarters Since Tornado}") legend(size(small)) xtitle(, margin(small))  
	xlabel(-12(2)12) scheme(s2mono) plotregion(fcolor(white)) graphregion(fcolor(white)); 
	graph export "${maindir}/figures/figure2_`v'_`sample'_`tsample'.pdf", as(pdf) replace;

restore;
};

local var "fstart_cond";

foreach v of local var {;
preserve;
keep `v'*  qsince;
label var `v'01 "Nearby-Aid";
label var `v'11 "Hit-Aid";
label var `v'00 "Nearby-No Aid";
label var `v'10 "Hit-No Aid";

gen `v'_ia_d = `v'11-`v'01;
gen `v'_nia_d = `v'10-`v'00;

label var `v'_ia_d "Individual Assistance(IA)";
label var `v'_nia_d "No IA";


twoway (connected `v'00 qsince, lcolor(blue) msymbol(triangle_hollow) mcolor(blue) lpattern(dash)) 
    (connected `v'01 qsince, lcolor(red) msymbol(circle_hollow) mcolor(red))
	(connected `v'10 qsince, lcolor(dkgreen) msymbol(triangle) mcolor(dkgreen) lpattern(dash) ) 
    (connected `v'11 qsince, lcolor(dkorange) msymbol(circle) mcolor(dkorange)), 
	ytitle("{bf:Foreclosure (Conditional)}") xline(-1, lcolor(black)) ytitle(, margin(small)) xtitle("{bf:Quarters Since Tornado}") legend(size(small)) xtitle(, margin(small))  
	xlabel(-12(2)12) scheme(s2mono) plotregion(fcolor(white)) graphregion(fcolor(white));
	graph export "${maindir}/figures/appfigure2_`v'_`sample'_`tsample'.pdf", as(pdf) replace;

restore;
};

local var "r_numautoopen";

foreach v of local var {;
preserve;
keep `v'*  qsince;
label var `v'01 "Nearby-Aid";
label var `v'11 "Hit-Aid";
label var `v'00 "Nearby-No Aid";
label var `v'10 "Hit-No Aid";

gen `v'_ia_d = `v'11-`v'01;
gen `v'_nia_d = `v'10-`v'00;

label var `v'_ia_d "Aid";
label var `v'_nia_d "No Aid";


twoway (connected `v'00 qsince, lcolor(blue) msymbol(triangle_hollow) mcolor(blue) lpattern(dash)) 
    (connected `v'01 qsince, lcolor(red) msymbol(circle_hollow) mcolor(red))
	(connected `v'10 qsince, lcolor(dkgreen) msymbol(triangle) mcolor(dkgreen) lpattern(dash) ) 
    (connected `v'11 qsince, lcolor(dkorange) msymbol(circle) mcolor(dkorange)), 
	ytitle("{bf:Foreclosure (Conditional)}") xline(-1, lcolor(black)) ytitle(, margin(small)) xtitle("{bf:Quarters Since Tornado}") legend(size(small)) xtitle(, margin(small))  
	xlabel(-12(2)12) scheme(s2mono) plotregion(fcolor(white)) graphregion(fcolor(white));
	graph save "${maindir}/figures/figure_`v'_`sample'_`tsample'", replace;
	graph export "${maindir}/figures/figure_`v'_`sample'_`tsample'.pdf", as(pdf) replace;

twoway (connected `v'_nia_d qsince, lcolor(blue) msymbol(triangle) mcolor(blue) lpattern(dash)) 
    (connected `v'_ia_d qsince, lcolor(red) msymbol(circle) mcolor(red)), 
	ytitle("{bf:Difference in Hit Minus Nearby New Auto Purchases}") xline(-1, lcolor(black)) ytitle(, margin(small)) xtitle("{bf:Quarters Since Tornado}") legend(size(small)) xtitle(, margin(small))  
	xlabel(-12(2)12) scheme(s2mono) plotregion(fcolor(white)) graphregion(fcolor(white));
	graph save "${maindir}/figures/appfigure6_`v'_diff_`sample'_`tsample'", replace;
	graph export "${maindir}/figures/appfigure6_`v'_diff_`sample'_`tsample'.pdf", as(pdf) replace;
	
restore;
};

};
* end of making Credit Card Mean Figure;


* begin combining figures from business sales and auto purchases;
if(`combinebusandccpfigs'==1)
{;
*graph use ${maindir}/figures/ln_sales_all_residual_DD_g3treat.gph;
*graph edit ${maindir}/figures/ln_sales_all_residual_DD_g3treat_ytitle.gph, ytitle("{bf:Difference in Hit Minus Nearby Log(Establishment Sales)}");
*graph save ${maindir}/figures/ln_sales_all_residual_DD_g3treat_ytitle.gph, replace;

grc1leg ${maindir}/figures/appfigure6_r_numautoopen_diff_`sample'_`tsample'.gph ${maindir}/figures/ln_sales_all_residual_DD_g3treat_ytitle.gph, col(2);
graph export "${maindir}/figures/appendixfigure6_`sample'_`tsample'.pdf", as(pdf) replace;
* ytitle("{bf:Difference in Hit Minus Nearby Log(Establishment Sales)}");
};




* OLS Eventstudies;
* beginning of making Figure 3;
if(`makefigure3'==1) 
{;
* Figure 3 Regressions;

openregulardataset `sample' `tsample';


* dd spec eventstudy figure;
clear matrix;

foreach var in `figure3vars' 
{;
	* Event study dd spec - Damage Heterogeneity Version - Annual Estimates;

	xtreg `var'
	ys3m_efwt ys2m_efwt qsp0_efwt ys1p_efwt ys2p_efwt ys3p_efwt
	ys3m ys2m qsp0 ys1p ys2p ys3p
	weighted_intensity  _Iqdate*, robust i(ncid) fe cluster(id);

	matrix tbl=r(table);
	matrix evstud1=[-3,tbl["b","ys3m_efwt"],tbl["ll","ys3m_efwt"],tbl["ul","ys3m_efwt"]];
	matrix evstud1=nullmat(evstud1)\[-2,tbl["b","ys2m_efwt"],tbl["ll","ys2m_efwt"],tbl["ul","ys2m_efwt"]];
	matrix evstud1=nullmat(evstud1)\[-1,0,0,0];
	matrix evstud1=nullmat(evstud1)\[0,tbl["b","qsp0_efwt"],tbl["ll","qsp0_efwt"],tbl["ul","qsp0_efwt"]];
	matrix evstud1=nullmat(evstud1)\[1,tbl["b","ys1p_efwt"],tbl["ll","ys1p_efwt"],tbl["ul","ys1p_efwt"]];
	matrix evstud1=nullmat(evstud1)\[2,tbl["b","ys2p_efwt"],tbl["ll","ys2p_efwt"],tbl["ul","ys2p_efwt"]];
	matrix evstud1=nullmat(evstud1)\[3,tbl["b","ys3p_efwt"],tbl["ll","ys3p_efwt"],tbl["ul","ys3p_efwt"]];
	matrix colname evstud1 = t`var' coef`var' ci_l_`var' ci_h_`var';
	matrix figure3_`sample'_`tsample' = [nullmat(figure3_`sample'_`tsample'), evstud1];
	cap noisily test ys3m_efwt ys2m_efwt;

};
matsave figure3_`sample'_`tsample', saving replace path("${maindir}/tmp_data/");


* create v1 version with q0 broken out on its own;
use "${maindir}/tmp_data/figure3_`sample'_`tsample'.dta", clear;
foreach var in `figure3vars' {;
  if "`var'" == "bal_bcret_tot" {;
	local title "A: Credit Card Debt";
	local ytitle "";
	local xtitle "Years Since Tornado";
	};
  else if "`var'" == "bal_home_tot_cond" {;
	local title "B: Home Debt (Cond'l)";
	local ytitle "";
	local xtitle "Years Since Tornado";
	};
  else if "`var'" == "bal_auto_tot" {;
	local title "C: Auto Debt";
	local ytitle "";
	local xtitle "Years Since Tornado";
	};	
  else if "`var'" == "bal_other2_tot" {;
	local title "D: Other Debt";
	local ytitle "";
	local xtitle "Years Since Tornado";
	};			
  else if "`var'" == "riskscore" {;
	local title "E: Equifax Risk Score";
	local ytitle "";
	local xtitle "Years Since Tornado";
	};	
  else if "`var'" == "gt1_all_90" {;
	local title "F: 90 Day Delinquency";
	local ytitle "";
	local xtitle "Years Since Tornado";
	};	
  twoway (sc coef`var' t`var', msymbol(S) msize(small) mcolor(blue) lcolor(blue) connect(none)) 
		(rcap ci_h_`var' ci_l_`var' t`var', sort lpattern(solid) lcolor(blue))  
		,legend(off) ylabel(, labsize(vsmall)) 
		title(`title', size(small)) ytitle("{bf:`ytitle'}", size(small)) xtitle("{bf: `xtitle'}", size(small)) 
		xline(-0.5,lpattern(solid) lcolor(maroon%80)) xmtick(-3 -2 -1 0 1 2 3) xlabel(-3 -2 -1 0 1 2 3, norescale)   yline(0,lpattern(dash) lcolor(gray)) 
		scheme(s2mono) plotregion(fcolor(white)) graphregion(fcolor(white)) saving("${maindir}/figures/`var'_figure3_`sample'_`tsample'_YRS", replace);		
};

};
* end of making Figure 3;




* beginning of making migration Figure 3;
if(`makemigfigure3'==1) 
{;
* Figure 3 migration Regressions;

openmigrationdataset `sample' `tsample';


* dd spec eventstudy figure;
clear matrix;

foreach var in `figure3migvars' 
{;
	* Event study dd spec - Damage Heterogeneity Version - Annual Estimates;

	xtreg `var'
	ys3m_efwt ys2m_efwt qsp0_efwt ys1p_efwt ys2p_efwt ys3p_efwt
	ys3m ys2m qsp0 ys1p ys2p ys3p
	weighted_intensity  _Iqdate*, robust i(ncid) fe cluster(id);

	matrix tbl=r(table);
	matrix evstud1=[-3,tbl["b","ys3m_efwt"],tbl["ll","ys3m_efwt"],tbl["ul","ys3m_efwt"]];
	matrix evstud1=nullmat(evstud1)\[-2,tbl["b","ys2m_efwt"],tbl["ll","ys2m_efwt"],tbl["ul","ys2m_efwt"]];
	matrix evstud1=nullmat(evstud1)\[-1,0,0,0];
	matrix evstud1=nullmat(evstud1)\[0,tbl["b","qsp0_efwt"],tbl["ll","qsp0_efwt"],tbl["ul","qsp0_efwt"]];
	matrix evstud1=nullmat(evstud1)\[1,tbl["b","ys1p_efwt"],tbl["ll","ys1p_efwt"],tbl["ul","ys1p_efwt"]];
	matrix evstud1=nullmat(evstud1)\[2,tbl["b","ys2p_efwt"],tbl["ll","ys2p_efwt"],tbl["ul","ys2p_efwt"]];
	matrix evstud1=nullmat(evstud1)\[3,tbl["b","ys3p_efwt"],tbl["ll","ys3p_efwt"],tbl["ul","ys3p_efwt"]];
	matrix colname evstud1 = t`var' coef`var' ci_l_`var' ci_h_`var';
	matrix figure3migv1_`sample'_`tsample' = [nullmat(figure3migv1_`sample'_`tsample'), evstud1];
	cap noisily test ys3m_efwt ys2m_efwt;

};
matsave figure3migv1_`sample'_`tsample', saving replace path("${maindir}/tmp_data/");


* create v1 version with q0 broken out on its own;
use "${maindir}/tmp_data/figure3migv1_`sample'_`tsample'.dta", clear;
foreach var in `figure3migvars' {;
  if "`var'" == "moveblock" {;
	local title "G: Move from Block 1 Q";
	local ytitle "";
	local xtitle "Years Since Tornado";
	};
  else if "`var'" == "movecounty" {;
	local title "G: Move from County 1 Quarter";
	local ytitle "";
	local xtitle "Years Since Tornado";
	};
  else if "`var'" == "moveblock3yr" {;
	local title "H: Move from Block 3 Yrs";
	local ytitle "";
	local xtitle "Years Since Tornado";
	};	
  else if "`var'" == "movecounty3yr" {;
	local title "H: Move from County 3 Years";
	local ytitle "";
	local xtitle "Years Since Tornado";
	};			
  twoway (sc coef`var' t`var', msymbol(S) msize(small) mcolor(blue) lcolor(blue) connect(none)) 
		(rcap ci_h_`var' ci_l_`var' t`var', sort lpattern(solid) lcolor(blue))  
		, legend(off) ylabel(, labsize(vsmall)) 
		title(`title', size(small)) ytitle("{bf:`ytitle'}", size(small)) xtitle("{bf: `xtitle'}", size(small)) 
		xline(-0.5,lpattern(solid) lcolor(maroon%80)) xmtick(-3 -2 -1 0 1 2 3) xlabel(-3 -2 -1 0 1 2 3, norescale)   yline(0,lpattern(dash) lcolor(gray)) 
		scheme(s2mono) plotregion(fcolor(white)) graphregion(fcolor(white)) saving("${maindir}/figures/`var'_figure3migv1_`sample'_`tsample'_YRS", replace);
};

};
* end of making migration Figure 3;







* OLS Eventstudies;
* beginning of making Figure 5;
if(`makefigure5'==1) 
{;
* Figure 5 Regressions;

openregulardataset `sample' `tsample';


* dd spec eventstudy figure;
clear matrix;

foreach var in `figure5vars' 
{;
	* Event study ddd spec - Damage Heterogeneity Version - Annual Estimates;

	xtreg `var' 
	ys3m_efwt_ia ys2m_efwt_ia qsp0_efwt_ia ys1p_efwt_ia ys2p_efwt_ia ys3p_efwt_ia  
	ys3m_efwt ys2m_efwt qsp0_efwt ys1p_efwt ys2p_efwt ys3p_efwt 
	ys3m_ia ys2m_ia qsp0_ia ys1p_ia ys2p_ia ys3p_ia 
	ys3m ys2m qsp0 ys1p ys2p ys3p 
	weighted_intensity ia ia_wgtintens  _Iqdate*, robust i(ncid) fe cluster(id);

	matrix tbl=r(table);
	matrix evstud1=[-3,tbl["b","ys3m_efwt_ia"],tbl["ll","ys3m_efwt_ia"],tbl["ul","ys3m_efwt_ia"]];
	matrix evstud1=nullmat(evstud1)\[-2,tbl["b","ys2m_efwt_ia"],tbl["ll","ys2m_efwt_ia"],tbl["ul","ys2m_efwt_ia"]];
	matrix evstud1=nullmat(evstud1)\[-1,0,0,0];
	matrix evstud1=nullmat(evstud1)\[0,tbl["b","qsp0_efwt_ia"],tbl["ll","qsp0_efwt_ia"],tbl["ul","qsp0_efwt_ia"]];
	matrix evstud1=nullmat(evstud1)\[1,tbl["b","ys1p_efwt_ia"],tbl["ll","ys1p_efwt_ia"],tbl["ul","ys1p_efwt_ia"]];
	matrix evstud1=nullmat(evstud1)\[2,tbl["b","ys2p_efwt_ia"],tbl["ll","ys2p_efwt_ia"],tbl["ul","ys2p_efwt_ia"]];
	matrix evstud1=nullmat(evstud1)\[3,tbl["b","ys3p_efwt_ia"],tbl["ll","ys3p_efwt_ia"],tbl["ul","ys3p_efwt_ia"]];
	matrix colname evstud1 = t`var' coef`var' ci_l_`var' ci_h_`var';
	matrix figure5_`sample'_`tsample' = [nullmat(figure5_`sample'_`tsample'), evstud1];
	cap noisily test ys3m_efwt_ia ys2m_efwt_ia;

};
matsave figure5_`sample'_`tsample', saving replace path("${maindir}/tmp_data/");


* create v1 version with q0 broken out on its own;
use "${maindir}/tmp_data/figure5_`sample'_`tsample'.dta", clear;
foreach var in `figure5vars' {;
  if "`var'" == "bal_bcret_tot" {;
	local title "A: Credit Card Debt";
	local ytitle "";
	local xtitle "Years Since Tornado";
	};
  else if "`var'" == "bal_home_tot_cond" {;
	local title "B: Home Debt (Cond'l)";
	local ytitle "";
	local xtitle "Years Since Tornado";
	};
  else if "`var'" == "bal_auto_tot" {;
	local title "C: Auto Debt";
	local ytitle "";
	local xtitle "Years Since Tornado";
	};	
  else if "`var'" == "bal_other2_tot" {;
	local title "D: Other Debt";
	local ytitle "";
	local xtitle "Years Since Tornado";
	};			
  else if "`var'" == "riskscore" {;
	local title "E: Equifax Risk Score";
	local ytitle "";
	local xtitle "Years Since Tornado";
	};	
  else if "`var'" == "gt1_all_90" {;
	local title "F: 90 Day Delinquency";
	local ytitle "";
	local xtitle "Years Since Tornado";
	};	
  twoway (sc coef`var' t`var', msymbol(S) msize(small) mcolor(blue) lcolor(blue) connect(none)) 
		(rcap ci_h_`var' ci_l_`var' t`var', sort lpattern(solid) lcolor(blue))  
		,legend(off) ylabel(, labsize(vsmall)) 
		title(`title', size(small)) ytitle("{bf:`ytitle'}", size(small)) xtitle("{bf: `xtitle'}", size(small)) 
		xline(-0.5,lpattern(solid) lcolor(maroon%80)) xmtick(-3 -2 -1 0 1 2 3) xlabel(-3 -2 -1 0 1 2 3, norescale)   yline(0,lpattern(dash) lcolor(gray)) 
		scheme(s2mono) plotregion(fcolor(white)) graphregion(fcolor(white)) saving("${maindir}/figures/`var'_figure5_`sample'_`tsample'_YRS", replace);		
};


};
* end of making Figure 5;





* beginning of making migration Figure 5;
if(`makemigfigure5'==1) 
{;
* Figure 5 migration Regressions;

openmigrationdataset `sample' `tsample';


* dd spec eventstudy figure;
clear matrix;

foreach var in `figure5migvars' 
{;

	* Event study ddd spec - Damage Heterogeneity Version - Annual Estimates;

	xtreg `var' ys3m_efwt_ia ys2m_efwt_ia qsp0_efwt_ia ys1p_efwt_ia ys2p_efwt_ia ys3p_efwt_ia  
	ys3m_efwt ys2m_efwt qsp0_efwt ys1p_efwt ys2p_efwt ys3p_efwt 
	ys3m_ia ys2m_ia qsp0_ia ys1p_ia ys2p_ia ys3p_ia 
	ys3m ys2m qsp0 ys1p ys2p ys3p 
	weighted_intensity ia ia_wgtintens  _Iqdate*, robust i(ncid) fe cluster(id);

	matrix tbl=r(table);
	matrix evstud1=[-3,tbl["b","ys3m_efwt_ia"],tbl["ll","ys3m_efwt_ia"],tbl["ul","ys3m_efwt_ia"]];
	matrix evstud1=nullmat(evstud1)\[-2,tbl["b","ys2m_efwt_ia"],tbl["ll","ys2m_efwt_ia"],tbl["ul","ys2m_efwt_ia"]];
	matrix evstud1=nullmat(evstud1)\[-1,0,0,0];
	matrix evstud1=nullmat(evstud1)\[0,tbl["b","qsp0_efwt_ia"],tbl["ll","qsp0_efwt_ia"],tbl["ul","qsp0_efwt_ia"]];
	matrix evstud1=nullmat(evstud1)\[1,tbl["b","ys1p_efwt_ia"],tbl["ll","ys1p_efwt_ia"],tbl["ul","ys1p_efwt_ia"]];
	matrix evstud1=nullmat(evstud1)\[2,tbl["b","ys2p_efwt_ia"],tbl["ll","ys2p_efwt_ia"],tbl["ul","ys2p_efwt_ia"]];
	matrix evstud1=nullmat(evstud1)\[3,tbl["b","ys3p_efwt_ia"],tbl["ll","ys3p_efwt_ia"],tbl["ul","ys3p_efwt_ia"]];
	matrix colname evstud1 = t`var' coef`var' ci_l_`var' ci_h_`var';
	matrix figure5migv1_`sample'_`tsample' = [nullmat(figure5migv1_`sample'_`tsample'), evstud1];
	cap noisily test ys3m_efwt_ia ys2m_efwt_ia;

};
matsave figure5migv1_`sample'_`tsample', saving replace path("${maindir}/tmp_data/");

* create v1 version with q0 broken out on its own;
use "${maindir}/tmp_data/figure5migv1_`sample'_`tsample'.dta", clear;
foreach var in `figure5migvars' {;
  if "`var'" == "moveblock" {;
	local title "G: Move from Block 1 Q";
	local ytitle "";
	local xtitle "Years Since Tornado";
	};
  else if "`var'" == "movecounty" {;
	local title "G: Move from County 1 Quarter";
	local ytitle "";
	local xtitle "Years Since Tornado";
	};
  else if "`var'" == "moveblock3yr" {;
	local title "H: Move from Block 3 Yrs";
	local ytitle "";
	local xtitle "Years Since Tornado";
	};	
  else if "`var'" == "movecounty3yr" {;
	local title "H: Move from County 3 Years";
	local ytitle "";
	local xtitle "Years Since Tornado";
	};			
  twoway (sc coef`var' t`var', msymbol(S) msize(small) mcolor(blue) lcolor(blue) connect(none)) 
		(rcap ci_h_`var' ci_l_`var' t`var', sort lpattern(solid) lcolor(blue))  
		, legend(off) ylabel(, labsize(vsmall)) 
		title(`title', size(small)) ytitle("{bf:`ytitle'}", size(small)) xtitle("{bf: `xtitle'}", size(small)) 
		xline(-0.5,lpattern(solid) lcolor(maroon%80)) xmtick(-3 -2 -1 0 1 2 3) xlabel(-3 -2 -1 0 1 2 3, norescale)   yline(0,lpattern(dash) lcolor(gray)) 
		scheme(s2mono) plotregion(fcolor(white)) graphregion(fcolor(white)) saving("${maindir}/figures/`var'_figure5migv1_`sample'_`tsample'_YRS", replace);
};

};
* end of making migration Figure 5;




* 2x4 Panel of OLS Eventstudy Plots;

if(`olseventstudyplots2x4'==1) {;

	* create v1 version with q0 broken out on its own;
	capture noisily {;
		gr combine ${maindir}/figures/bal_bcret_tot_figure3_`sample'_`tsample'_YRS.gph	
		   ${maindir}/figures/bal_home_tot_cond_figure3_`sample'_`tsample'_YRS.gph
		   ${maindir}/figures/bal_auto_tot_figure3_`sample'_`tsample'_YRS.gph
		   ${maindir}/figures/bal_other2_tot_figure3_`sample'_`tsample'_YRS.gph 
		   ${maindir}/figures/riskscore_figure3_`sample'_`tsample'_YRS.gph
		   ${maindir}/figures/gt1_all_90_figure3_`sample'_`tsample'_YRS.gph
		   ${maindir}/figures/moveblock_figure3migv1_`sample'_`tsample'_YRS.gph
		   ${maindir}/figures/moveblock3yr_figure3migv1_`sample'_`tsample'_YRS.gph, 
		   col(4) scheme(s1mono) xsize(16) ysize(8);
		graph export "${maindir}/figures/figure3_ols_alltreated_`sample'_`tsample'.pdf", as(pdf) replace;
	};
	* end of create v1 version;

	* create v1 version with q0 broken out on its own;
	capture noisily {;
		gr combine ${maindir}/figures/bal_bcret_tot_figure5_`sample'_`tsample'_YRS.gph	
		   ${maindir}/figures/bal_home_tot_cond_figure5_`sample'_`tsample'_YRS.gph
		   ${maindir}/figures/bal_auto_tot_figure5_`sample'_`tsample'_YRS.gph
		   ${maindir}/figures/bal_other2_tot_figure5_`sample'_`tsample'_YRS.gph 
		   ${maindir}/figures/riskscore_figure5_`sample'_`tsample'_YRS.gph
		   ${maindir}/figures/gt1_all_90_figure5_`sample'_`tsample'_YRS.gph
		   ${maindir}/figures/moveblock_figure5migv1_`sample'_`tsample'_YRS.gph
		   ${maindir}/figures/moveblock3yr_figure5migv1_`sample'_`tsample'_YRS.gph, 
		   col(4) scheme(s1mono) xsize(16) ysize(8);
		graph export "${maindir}/figures/figure5_ols_alltreated_`sample'_`tsample'.pdf", as(pdf) replace;
	};
	* end of create v1 version;
};
* end of if;

* 2x4 Panel of OLS Eventstudy Plots;




local sample = "near_05_15";
local tsample = "shawn";

disp("Sample: `sample'");
disp("T-Sample: `tsample'");


* beginning of making Table1;
if(`maketable1'==1) 
{;
* Table 1 Regressions;

openregulardataset `sample' `tsample';


* dd spec post only.  No pre-test - Pooled Version;
* Copy table template;
copy "${maindir}/table templates/paper/Table1_template.xlsx" "${maindir}/results/table1_`sample'_`tsample'.xlsx", replace public;
putexcel set "${maindir}/results/table1_`sample'_`tsample'.xlsx", modify sheet("template");

clear matrix;

foreach var in `table1vars' 
{;
	xtreg `var' post_wgtintens post weighted_intensity 
	_Iqdate*, robust i(ncid) fe cluster(id);
	mat cola = [_b[post_wgtintens] \ _se[post_wgtintens]];
	sum `var' if e(sample)==1 & qsince==-1 & treated==1;
	mat cola = [cola \ `r(mean)' \ e(blank) \ e(r2_o) \ e(N)];
	mat panela = [nullmat(panela), cola];

	xtreg `var' post_ef_group1 post_ef_group2 post_ef_group3 post ef_group1 ef_group2 ef_group3 
	 _Iqdate*, robust i(ncid) fe cluster(id);
	mat colb = [_b[post_ef_group1] \ _se[post_ef_group1]];
	sum `var' if e(sample)==1 & ef_group1==1 & qsince==-1 & treated==1;
	mat colb = [colb \ `r(mean)' \ e(blank)];

	mat colb = [colb \ _b[post_ef_group2] \ _se[post_ef_group2]];
	sum `var' if e(sample)==1 & ef_group2==1 & qsince==-1 & treated==1;
	mat colb = [colb \ `r(mean)' \ e(blank)];

	mat colb = [colb \ _b[post_ef_group3] \ _se[post_ef_group3]];
	sum `var' if e(sample)==1 & ef_group3==1 & qsince==-1 & treated==1;
	mat colb = [colb \ `r(mean)' \ e(blank)];
	mat colb = [colb \ e(r2_o) \ e(N)];
	mat panelb = [nullmat(panelb), colb];

};

putexcel B9 = matrix(panela);
putexcel B18 = matrix(panelb);

putexcel save;



openmigrationdataset `sample' `tsample';


* dd spec post only.  No pre-test - Pooled Version;
putexcel set "${maindir}/results/table1_`sample'_`tsample'.xlsx", modify sheet("template");

clear matrix;

foreach var in `table1migvars' 
{;
	xtreg `var' post_wgtintens post weighted_intensity 
	_Iqdate*, robust i(ncid) fe cluster(id);
	mat cola = [_b[post_wgtintens] \ _se[post_wgtintens]];
	sum `var' if e(sample)==1 & qsince==-1 & treated==1;
	mat cola = [cola \ `r(mean)' \ e(blank) \ e(r2_o) \ e(N)];
	mat panela = [nullmat(panela), cola];

	xtreg `var' post_ef_group1 post_ef_group2 post_ef_group3 post ef_group1 ef_group2 ef_group3 
	 _Iqdate*, robust i(ncid) fe cluster(id);
	mat colb = [_b[post_ef_group1] \ _se[post_ef_group1]];
	sum `var' if e(sample)==1 & ef_group1==1 & qsince==-1 & treated==1;
	mat colb = [colb \ `r(mean)' \ e(blank)];

	mat colb = [colb \ _b[post_ef_group2] \ _se[post_ef_group2]];
	sum `var' if e(sample)==1 & ef_group2==1 & qsince==-1 & treated==1;
	mat colb = [colb \ `r(mean)' \ e(blank)];

	mat colb = [colb \ _b[post_ef_group3] \ _se[post_ef_group3]];
	sum `var' if e(sample)==1 & ef_group3==1 & qsince==-1 & treated==1;
	mat colb = [colb \ `r(mean)' \ e(blank)];
	mat colb = [colb \ e(r2_o) \ e(N)];
	mat panelb = [nullmat(panelb), colb];

};

putexcel H9 = matrix(panela);
putexcel H18 = matrix(panelb);

putexcel save;

};
* end of making Table 1;




* beginning of making Table3;
if(`maketable3'==1) 
{;
* Table 3 Regressions;

openregulardataset `sample' `tsample';


* ddd spec post only.  No pre-test - Pooled Version;
* Copy table template;
copy "${maindir}/table templates/paper/Table3_template.xlsx" "${maindir}/results/table3_`sample'_`tsample'.xlsx", replace public;
putexcel set "${maindir}/results/table3_`sample'_`tsample'.xlsx", modify sheet("template");

clear matrix;

foreach var in `table3vars' 
{; 
	xtreg `var' post_ia_wgtintens post_ia post_wgtintens post weighted_intensity ia
	_Iqdate*, robust i(ncid) fe cluster(id);
	mat cola = [_b[post_ia_wgtintens] \ _se[post_ia_wgtintens]];
	sum `var' if e(sample)==1 & qsince==-1 & treated==1 & ia==1;
	mat cola = [cola \ `r(mean)' \ e(blank) \ e(r2_o) \ e(N)];
	mat panela = [nullmat(panela), cola];

	xtreg `var' post_ia_ef_group1 post_ia_ef_group2 post_ia_ef_group3 post_ia post_ef_group1 post_ef_group2 post_ef_group3 post ef_group1 ef_group2 ef_group3 ia
	 _Iqdate*, robust i(ncid) fe cluster(id);
	mat colb = [_b[post_ia_ef_group1] \ _se[post_ia_ef_group1]];
	sum `var' if e(sample)==1 & ef_group1==1 & qsince==-1 & treated==1 & ia==1;
	mat colb = [colb \ `r(mean)' \ e(blank)];

	mat colb = [colb \ _b[post_ia_ef_group2] \ _se[post_ia_ef_group2]];
	sum `var' if e(sample)==1 & ef_group2==1 & qsince==-1 & treated==1 & ia==1;
	mat colb = [colb \ `r(mean)' \ e(blank)];

	mat colb = [colb \ _b[post_ia_ef_group3] \ _se[post_ia_ef_group3]];
	sum `var' if e(sample)==1 & ef_group3==1 & qsince==-1 & treated==1 & ia==1;
	mat colb = [colb \ `r(mean)' \ e(blank)];
	mat colb = [colb \ e(r2_o) \ e(N)];
	mat panelb = [nullmat(panelb), colb];


};

putexcel B9 = matrix(panela);
putexcel B18 = matrix(panelb);

putexcel save;


* Table 3 Migration Regressions;

openmigrationdataset `sample' `tsample';


* ddd spec post only.  No pre-test - Pooled Version;
putexcel set "${maindir}/results/table3_`sample'_`tsample'.xlsx", modify sheet("template");

clear matrix;

foreach var in `table3migvars' 
{; 
	xtreg `var' post_ia_wgtintens post_ia post_wgtintens post weighted_intensity ia
	_Iqdate*, robust i(ncid) fe cluster(id);
	mat cola = [_b[post_ia_wgtintens] \ _se[post_ia_wgtintens]];
	sum `var' if e(sample)==1 & qsince==-1 & treated==1 & ia==1;
	mat cola = [cola \ `r(mean)' \ e(blank) \ e(r2_o) \ e(N)];
	mat panela = [nullmat(panela), cola];

	xtreg `var' post_ia_ef_group1 post_ia_ef_group2 post_ia_ef_group3 post_ia post_ef_group1 post_ef_group2 post_ef_group3 post ef_group1 ef_group2 ef_group3 ia
	 _Iqdate*, robust i(ncid) fe cluster(id);
	mat colb = [_b[post_ia_ef_group1] \ _se[post_ia_ef_group1]];
	sum `var' if e(sample)==1 & ef_group1==1 & qsince==-1 & treated==1 & ia==1;
	mat colb = [colb \ `r(mean)' \ e(blank)];

	mat colb = [colb \ _b[post_ia_ef_group2] \ _se[post_ia_ef_group2]];
	sum `var' if e(sample)==1 & ef_group2==1 & qsince==-1 & treated==1 & ia==1;
	mat colb = [colb \ `r(mean)' \ e(blank)];

	mat colb = [colb \ _b[post_ia_ef_group3] \ _se[post_ia_ef_group3]];
	sum `var' if e(sample)==1 & ef_group3==1 & qsince==-1 & treated==1 & ia==1;
	mat colb = [colb \ `r(mean)' \ e(blank)];
	mat colb = [colb \ e(r2_o) \ e(N)];
	mat panelb = [nullmat(panelb), colb];


};

putexcel H9 = matrix(panela);
putexcel H18 = matrix(panelb);

putexcel save;
};
* end of making Table 3;






* beginning of making Table 4;
if(`maketable4'==1) 
{;
* Table 4 Regressions;

openregulardataset `sample' `tsample';


* ddd spec post only.  No pre-test - Pooled Version;
* Copy table template;
copy "${maindir}/table templates/paper/Table4_template.xlsx" "${maindir}/results/table4_`sample'_`tsample'.xlsx", replace public;
putexcel set "${maindir}/results/table4_`sample'_`tsample'.xlsx", modify sheet("template");

clear matrix;

foreach var in `table4vars' 
{; 
	xtreg `var'_lac post_ia_wgtintens post_ia post_wgtintens post weighted_intensity ia
	_Iqdate*, robust i(ncid) fe cluster(id);
	mat cola1 = [_b[post_ia_wgtintens] \ _se[post_ia_wgtintens]];
	sum `var'_lac if e(sample)==1 & qsince==-1 & treated==1 & ia==1;
	mat cola1 = [cola1 \ e(blank) \ `r(mean)' \ e(N)];
	mat panela1 = [nullmat(panela1), cola1];

	xtreg `var'_hac post_ia_wgtintens post_ia post_wgtintens post weighted_intensity ia
	_Iqdate*, robust i(ncid) fe cluster(id);
	mat cola2 = [_b[post_ia_wgtintens] \ _se[post_ia_wgtintens]];
	sum `var'_hac if e(sample)==1 & qsince==-1 & treated==1 & ia==1;
	mat cola2 = [cola2 \ e(blank) \ `r(mean)' \ e(N)];
	mat panela2 = [nullmat(panela2), cola2];

	xtreg `var'_lrs post_ia_wgtintens post_ia post_wgtintens post weighted_intensity ia
	_Iqdate*, robust i(ncid) fe cluster(id);
	mat colb1 = [_b[post_ia_wgtintens] \ _se[post_ia_wgtintens]];
	sum `var'_lrs if e(sample)==1 & qsince==-1 & treated==1 & ia==1;
	mat colb1 = [colb1 \ e(blank) \ `r(mean)' \ e(N)];
	mat panelb1 = [nullmat(panelb1), colb1];

	xtreg `var'_hrs post_ia_wgtintens post_ia post_wgtintens post weighted_intensity ia
	_Iqdate*, robust i(ncid) fe cluster(id);
	mat colb2 = [_b[post_ia_wgtintens] \ _se[post_ia_wgtintens]];
	sum `var'_hrs if e(sample)==1 & qsince==-1 & treated==1 & ia==1;
	mat colb2 = [colb2 \ e(blank) \ `r(mean)' \ e(N)];
	mat panelb2 = [nullmat(panelb2), colb2];

	xtreg `var'_you post_ia_wgtintens post_ia post_wgtintens post weighted_intensity ia
	_Iqdate*, robust i(ncid) fe cluster(id);
	mat colc1= [_b[post_ia_wgtintens] \ _se[post_ia_wgtintens]];
	sum `var'_you if e(sample)==1 & qsince==-1 & treated==1 & ia==1;
	mat colc1 = [colc1 \ e(blank) \ `r(mean)' \ e(N)];
	mat panelc1 = [nullmat(panelc1), colc1];

	xtreg `var'_old post_ia_wgtintens post_ia post_wgtintens post weighted_intensity ia
	_Iqdate*, robust i(ncid) fe cluster(id);
	mat colc2 = [_b[post_ia_wgtintens] \ _se[post_ia_wgtintens]];
	sum `var'_old if e(sample)==1 & qsince==-1 & treated==1 & ia==1;
	mat colc2 = [colc2 \ e(blank) \ `r(mean)' \ e(N)];
	mat panelc2 = [nullmat(panelc2), colc2];

};
putexcel B8 = matrix(panela1);
putexcel B15 = matrix(panela2);

putexcel B23 = matrix(panelb1);
putexcel B30 = matrix(panelb2);

putexcel save;


* Table 4 Migration Regressions;

openmigrationdataset `sample' `tsample';

putexcel set "${maindir}/results/table4_`sample'_`tsample'.xlsx", modify sheet("template");

clear matrix;

foreach var in `table4migvars' 
{; 
	xtreg `var'_lac post_ia_wgtintens post_ia post_wgtintens post weighted_intensity ia
	_Iqdate*, robust i(ncid) fe cluster(id);
	mat cola1 = [_b[post_ia_wgtintens] \ _se[post_ia_wgtintens]];
	sum `var'_lac if e(sample)==1 & qsince==-1 & treated==1 & ia==1;
	mat cola1 = [cola1 \ e(blank) \ `r(mean)' \ e(N)];
	mat panela1 = [nullmat(panela1), cola1];

	xtreg `var'_hac post_ia_wgtintens post_ia post_wgtintens post weighted_intensity ia
	_Iqdate*, robust i(ncid) fe cluster(id);
	mat cola2 = [_b[post_ia_wgtintens] \ _se[post_ia_wgtintens]];
	sum `var'_hac if e(sample)==1 & qsince==-1 & treated==1 & ia==1;
	mat cola2 = [cola2 \ e(blank) \ `r(mean)' \ e(N)];
	mat panela2 = [nullmat(panela2), cola2];

	xtreg `var'_lrs post_ia_wgtintens post_ia post_wgtintens post weighted_intensity ia
	_Iqdate*, robust i(ncid) fe cluster(id);
	mat colb1 = [_b[post_ia_wgtintens] \ _se[post_ia_wgtintens]];
	sum `var'_lrs if e(sample)==1 & qsince==-1 & treated==1 & ia==1;
	mat colb1 = [colb1 \ e(blank) \ `r(mean)' \ e(N)];
	mat panelb1 = [nullmat(panelb1), colb1];

	xtreg `var'_hrs post_ia_wgtintens post_ia post_wgtintens post weighted_intensity ia
	_Iqdate*, robust i(ncid) fe cluster(id);
	mat colb2 = [_b[post_ia_wgtintens] \ _se[post_ia_wgtintens]];
	sum `var'_hrs if e(sample)==1 & qsince==-1 & treated==1 & ia==1;
	mat colb2 = [colb2 \ e(blank) \ `r(mean)' \ e(N)];
	mat panelb2 = [nullmat(panelb2), colb2];

	xtreg `var'_you post_ia_wgtintens post_ia post_wgtintens post weighted_intensity ia
	_Iqdate*, robust i(ncid) fe cluster(id);
	mat colc1= [_b[post_ia_wgtintens] \ _se[post_ia_wgtintens]];
	sum `var'_you if e(sample)==1 & qsince==-1 & treated==1 & ia==1;
	mat colc1 = [colc1 \ e(blank) \ `r(mean)' \ e(N)];
	mat panelc1 = [nullmat(panelc1), colc1];

	xtreg `var'_old post_ia_wgtintens post_ia post_wgtintens post weighted_intensity ia
	_Iqdate*, robust i(ncid) fe cluster(id);
	mat colc2 = [_b[post_ia_wgtintens] \ _se[post_ia_wgtintens]];
	sum `var'_old if e(sample)==1 & qsince==-1 & treated==1 & ia==1;
	mat colc2 = [colc2 \ e(blank) \ `r(mean)' \ e(N)];
	mat panelc2 = [nullmat(panelc2), colc2];

};
putexcel F8 = matrix(panela1);
putexcel F15 = matrix(panela2);

putexcel F23 = matrix(panelb1);
putexcel F30 = matrix(panelb2);

putexcel save;
};
* end of making Table 4;





local tt = 1;
local sample = "near_05_15";
local tsample = "shawn";

disp("Samples: `samples'");
disp("Sample: `sample'");


* start of making Appendix Table 3: Aid/No-Aid Means;
if(`makeapptable3'==1) 
{;
* Appendix Table Cash/No-Cash Means;

openregulardataset `sample' `tsample';


* Copy table template;
copy "${maindir}/table templates/appendix/Appendix_Table3_template.xlsx" "${maindir}/results/appendix_table3_cash_nocash_means_`sample'_`tsample'.xlsx", replace public;
putexcel set "${maindir}/results/appendix_table3_cash_nocash_means_`sample'_`tsample'.xlsx", modify sheet("template");

clear matrix;

local counter = 1;
foreach var in `apptable3varspanela' 
{;
if(`counter' == 6) {;
	mat row = [e(blank), e(blank), e(blank), e(blank), e(blank), e(blank), e(blank), e(blank)];
	mat panela = [nullmat(panela) \ row];
};
	sum `var' if qsince==-1 & treated==1;
	mat row = [`r(mean)'];
	sum `var' if qsince==-1 & treated==0;
	mat row = [row, `r(mean)'];
	sum `var' if qsince==-1 & ia==1;
	mat row = [row, `r(mean)'];
	sum `var' if qsince==-1 & ia==1 & treated==1;
	mat row = [row, `r(mean)'];
	sum `var' if qsince==-1 & ia==1 & treated==0;
	mat row = [row, `r(mean)'];
	sum `var' if qsince==-1 & ia==0;
	mat row = [row, `r(mean)'];
	sum `var' if qsince==-1 & ia==0 & treated==1;
	mat row = [row, `r(mean)'];
	sum `var' if qsince==-1 & ia==0 & treated==0;
	mat row = [row, `r(mean)'];

	mat panela = [nullmat(panela) \ row];
	local counter = `counter' + 1;
};

foreach var in `apptable3varspanelb' 
{;
	sum `var' if qsince==-1 & treated==1;
	mat row = [`r(mean)'];
	sum `var' if qsince==-1 & treated==0;
	mat row = [row, `r(mean)'];
	sum `var' if qsince==-1 & ia==1;
	mat row = [row, `r(mean)'];
	sum `var' if qsince==-1 & ia==1 & treated==1;
	mat row = [row, `r(mean)'];
	sum `var' if qsince==-1 & ia==1 & treated==0;
	mat row = [row, `r(mean)'];
	sum `var' if qsince==-1 & ia==0;
	mat row = [row, `r(mean)'];
	sum `var' if qsince==-1 & ia==0 & treated==1;
	mat row = [row, `r(mean)'];
	sum `var' if qsince==-1 & ia==0 & treated==0;
	mat row = [row, `r(mean)'];
				
	mat panelb = [nullmat(panelb) \ row];
};

* keep only one observation per person;
keep if qsince==-1;

count;
count if cid~=cid[_n-1];

sort cid;
count if cid~=cid[_n-1] & treated==1;
mat rowpeople = [`r(N)'];

count if cid~=cid[_n-1] & treated==0;
mat rowpeople = [rowpeople, `r(N)'];

count if cid~=cid[_n-1] & ia==1;
mat rowpeople = [rowpeople, `r(N)'];

count if cid~=cid[_n-1] & treated==1 & ia==1;
mat rowpeople = [rowpeople, `r(N)'];

count if cid~=cid[_n-1] & treated==0 & ia==1;
mat rowpeople = [rowpeople, `r(N)'];

count if cid~=cid[_n-1] & ia==0;
mat rowpeople = [rowpeople, `r(N)'];

count if cid~=cid[_n-1] & treated==1 & ia==0;
mat rowpeople = [rowpeople, `r(N)'];

count if cid~=cid[_n-1] & treated==0 & ia==0;
mat rowpeople = [rowpeople, `r(N)'];


sort block_str cid;
drop if block_str==block_str[_n-1];

count if treated==1;
mat rowblocks = [`r(N)'];

count if treated==0;
mat rowblocks = [rowblocks, `r(N)'];

count if ia==1;
mat rowblocks = [rowblocks, `r(N)'];

count if treated==1 & ia==1;
mat rowblocks = [rowblocks, `r(N)'];

count if treated==0 & ia==1;
mat rowblocks = [rowblocks, `r(N)'];

count if ia==0;
mat rowblocks = [rowblocks, `r(N)'];

count if treated==1 & ia==0;
mat rowblocks = [rowblocks, `r(N)'];

count if treated==0 & ia==0;
mat rowblocks = [rowblocks, `r(N)'];

mat panelc = [rowpeople \ rowblocks]; 


putexcel B8 = matrix(panela);
putexcel B20 = matrix(panelb);
putexcel B31 = matrix(panelc);

putexcel save;

};
* end of making Appendix Table 3: Cash/No-Cash Means;




* beginning of making Appendix Table 4;
if(`makeapptable4'==1) 
{;
* Appendix Table 4 Means;

openregulardataset `sample' `tsample';


* Copy table template;
copy "${maindir}/table templates/appendix/Appendix_Table4_template.xlsx" "${maindir}/results/appendix_table4_`sample'_`tsample'.xlsx", replace public;
putexcel set "${maindir}/results/appendix_table4_`sample'_`tsample'.xlsx", modify sheet("template");

clear matrix;

local counter = 1;
foreach var in `apptable3varspanela' 
{;
if(`counter' == 6) {;
	mat row = [e(blank), e(blank), e(blank), e(blank), e(blank), e(blank), e(blank), e(blank), e(blank)];
	mat panela = [nullmat(panela) \ row];
};
	sum `var' if qsince==-1 & ef_group1==1;
	mat row = [`r(mean)', `r(sd)'];
	sum `var' if qsince==-1 & ef_group2==1;
	mat row = [row, `r(mean)', `r(sd)'];
	sum `var' if qsince==-1 & ef_group3==1;
	mat row = [row, `r(mean)', `r(sd)'];

	sum `var' if qsince==-1 & (ef_group1==1 | ef_group2==1);
	mat row = [row, `r(sd)'];
	sum `var' if qsince==-1 & (ef_group1==1 | ef_group3==1);
	mat row = [row, `r(sd)'];
	sum `var' if qsince==-1 & (ef_group2==1 | ef_group3==1);
	mat row = [row, `r(sd)'];
		
	mat panela = [nullmat(panela) \ row];
	local counter = `counter' + 1;
};

local counter = 1;
foreach var in `apptable3varspanelb' 
{;
	sum `var' if qsince==-1 & ef_group1==1;
	mat row = [`r(mean)', `r(sd)'];
	sum `var' if qsince==-1 & ef_group2==1;
	mat row = [row, `r(mean)', `r(sd)'];
	sum `var' if qsince==-1 & ef_group3==1;
	mat row = [row, `r(mean)', `r(sd)'];
	
	sum `var' if qsince==-1 & (ef_group1==1 | ef_group2==1);
	mat row = [row, `r(sd)'];
	sum `var' if qsince==-1 & (ef_group1==1 | ef_group3==1);
	mat row = [row, `r(sd)'];
	sum `var' if qsince==-1 & (ef_group2==1 | ef_group3==1);
	mat row = [row, `r(sd)'];
		
	mat panelb = [nullmat(panelb) \ row];
};

* keep only one observation per person;
keep if qsince==-1;

sort cid;
count if ef_group1==1;
mat rowpeople = [`r(N)', e(blank)];
count if ef_group2==1;
mat rowpeople = [rowpeople, `r(N)', e(blank)];
count if ef_group3==1;
mat rowpeople = [rowpeople, `r(N)'];

sort block_str cid;
drop if block_str==block_str[_n-1];

count if ef_group1==1;
mat rowblocks = [`r(N)', e(blank)];
count if ef_group2==1;
mat rowblocks = [rowblocks, `r(N)', e(blank)];
count if ef_group3==1;
mat rowblocks = [rowblocks, `r(N)'];

mat panelc = [rowpeople \ rowblocks]; 


putexcel B7 = matrix(panela);
putexcel B18 = matrix(panelb);
putexcel B28 = matrix(panelc);

putexcel save;
};
* end of making Appendix Table 4;






local tt = 1;
local sample = "near_05_15";
local tsample = "shawn";


* beginning of making Appendix Table 6;
if(`makeapptable6'==1) 
{;
* Appendix Table 6 Regressions;

openregulardataset `sample' `tsample';


* dd spec post only.  No pre-test - Pooled Version;
* Copy table template;
copy "${maindir}/table templates/appendix/Appendix_Table6_template.xlsx" "${maindir}/results/appendix_table6_`sample'_`tsample'.xlsx", replace public;
putexcel set "${maindir}/results/appendix_table6_`sample'_`tsample'.xlsx", modify sheet("template");

clear matrix;

foreach var in `apptable6vars' 
{;
	preserve;
	keep if ia==1;
	
	* Aid ;
	xtreg `var' post_wgtintens post weighted_intensity 
	_Iqdate*, robust i(ncid) fe cluster(id);
	mat cola = [_b[post_wgtintens] \ _se[post_wgtintens]];
	sum `var' if e(sample)==1 & qsince==-1 & ia==1;
	mat cola = [cola \ `r(mean)' \ e(blank) \ e(r2_o) \ e(N)];
	mat panela = [nullmat(panela), cola];
	restore;

	preserve;
	keep if ia==0;
	
	* No Aid ;
	xtreg `var' post_wgtintens post weighted_intensity 
	_Iqdate*, robust i(ncid) fe cluster(id);
	mat cola = [_b[post_wgtintens] \ _se[post_wgtintens]];
	sum `var' if e(sample)==1 & qsince==-1 & ia==0;
	mat cola = [cola \ `r(mean)' \ e(blank) \ e(r2_o) \ e(N)];
	mat panela = [nullmat(panela), cola];
	restore;

	preserve;
	keep if ia==1;	

	* Aid ;
	xtreg `var' post_ef_group1 post_ef_group2 post_ef_group3 post ef_group1 ef_group2 ef_group3 
	 _Iqdate*, robust i(ncid) fe cluster(id);

	mat colb = [_b[post_ef_group1] \ _se[post_ef_group1]];
	
	sum `var' if e(sample)==1 & ef_group1==1 & qsince==-1 & ia==1;
	mat colb = [colb \ `r(mean)' \ e(blank)];
	
	mat colb = [colb \ _b[post_ef_group2] \ _se[post_ef_group2]];

	sum `var' if e(sample)==1 & ef_group2==1 & qsince==-1 & ia==1;
	mat colb = [colb \ `r(mean)' \ e(blank)];

	mat colb = [colb \ _b[post_ef_group3] \ _se[post_ef_group3]];
	
	sum `var' if e(sample)==1 & ef_group3==1 & qsince==-1 & ia==1;
	mat colb = [colb \ `r(mean)' \ e(blank) \ e(r2_o) \ e(N)];

	mat panelb = [nullmat(panelb), colb];
	restore;

	preserve;
	keep if ia==0;
	
	* No Aid ;
	xtreg `var' post_ef_group1 post_ef_group2 post_ef_group3 post ef_group1 ef_group2 ef_group3 
	 _Iqdate*, robust i(ncid) fe cluster(id);

	mat colb = [_b[post_ef_group1] \ _se[post_ef_group1]];
	
	sum `var' if e(sample)==1 & ef_group1==1 & qsince==-1 & ia==0;
	mat colb = [colb \ `r(mean)' \ e(blank)];
	
	mat colb = [colb \ _b[post_ef_group2] \ _se[post_ef_group2]];

	sum `var' if e(sample)==1 & ef_group2==1 & qsince==-1 & ia==0;
	mat colb = [colb \ `r(mean)' \ e(blank)];

	mat colb = [colb \ _b[post_ef_group3] \ _se[post_ef_group3]];
	
	sum `var' if e(sample)==1 & ef_group3==1 & qsince==-1 & ia==0;
	mat colb = [colb \ `r(mean)' \ e(blank) \ e(r2_o) \ e(N)];

	mat panelb = [nullmat(panelb), colb];
	restore;
};
putexcel B8 = matrix(panela);
putexcel B17 = matrix(panelb);

putexcel save;
};
* end of making Appendix Table 6;



* beginning of making Appendix Table 7;
if(`makeapptable7'==1) 
{;
* Appendix Table 7 Regressions;

openregulardataset `sample' `tsample';


* dd spec post only.  No pre-test - Pooled Version;
* Copy table template;
copy "${maindir}/table templates/appendix/Appendix_Table7_template.xlsx" "${maindir}/results/appendix_table7_`sample'_`tsample'.xlsx", replace public;
putexcel set "${maindir}/results/appendix_table7_`sample'_`tsample'.xlsx", modify sheet("template");

clear matrix;

foreach var in `apptable7vars' 
{;
	* Aid ;
	preserve;
	
	keep if ia == 1;
	xtreg `var' post_wgtintens post weighted_intensity 
	_Iqdate*, robust i(ncid) fe cluster(id);

	mat cola = [_b[post_wgtintens] \ _se[post_wgtintens]];
	sum `var' if e(sample)==1 & qsince==-1 & ia==1;
	mat cola = [cola \ `r(mean)' \ e(blank) \ e(r2_o) \ e(N)];
	mat panela = [nullmat(panela), cola];
	
	restore;

	* No Aid ;
	preserve;
	
	keep if ia == 0;
	xtreg `var' post_wgtintens post weighted_intensity 
	_Iqdate*, robust i(ncid) fe cluster(id);

	mat cola = [_b[post_wgtintens] \ _se[post_wgtintens]];
	sum `var' if e(sample)==1 & qsince==-1 & ia==0;
	mat cola = [cola \ `r(mean)' \ e(blank) \ e(r2_o) \ e(N)];
	mat panela = [nullmat(panela), cola];

	restore;

	* Aid ;
	preserve;
	
	keep if ia == 1;
	xtreg `var' post_ef_group1 post_ef_group2 post_ef_group3 post ef_group1 ef_group2 ef_group3 
	 _Iqdate*, robust i(ncid) fe cluster(id);
	 
	mat colb = [_b[post_ef_group1] \ _se[post_ef_group1]];	
	sum `var' if e(sample)==1 & ef_group1==1 & qsince==-1 & ia==1;
	mat colb = [colb \ `r(mean)' \ e(blank)];
	mat colb = [colb \ _b[post_ef_group2] \ _se[post_ef_group2]];	
	sum `var' if e(sample)==1 & ef_group2==1 & qsince==-1 & ia==1;
	mat colb = [colb \ `r(mean)' \ e(blank)];
	mat colb = [colb \ _b[post_ef_group3] \ _se[post_ef_group3]];
	sum `var' if e(sample)==1 & ef_group3==1 & qsince==-1 & ia==1;
	mat colb = [colb \ `r(mean)' \ e(blank) \ e(r2_o) \ e(N)];
	mat panelb = [nullmat(panelb), colb];

	restore;
	
	* No Aid ;
	preserve;
	
	keep if ia == 0;
	xtreg `var' post_ef_group1 post_ef_group2 post_ef_group3 post ef_group1 ef_group2 ef_group3 
	 _Iqdate*, robust i(ncid) fe cluster(id);

	mat colb = [_b[post_ef_group1] \ _se[post_ef_group1]];	
	sum `var' if e(sample)==1 & ef_group1==1 & qsince==-1 & ia==0;
	mat colb = [colb \ `r(mean)' \ e(blank)];
	mat colb = [colb \ _b[post_ef_group2] \ _se[post_ef_group2]];	
	sum `var' if e(sample)==1 & ef_group2==1 & qsince==-1 & ia==0;
	mat colb = [colb \ `r(mean)' \ e(blank)];
	mat colb = [colb \ _b[post_ef_group3] \ _se[post_ef_group3]];
	sum `var' if e(sample)==1 & ef_group3==1 & qsince==-1 & ia==0;
	mat colb = [colb \ `r(mean)' \ e(blank) \ e(r2_o) \ e(N)];
	mat panelb = [nullmat(panelb), colb];

	restore;
};
putexcel B8 = matrix(panela);
putexcel B17 = matrix(panelb);

* format the R^2;
putexcel B12:I12, overwritefmt nformat(#0.000);
putexcel B29:I29, overwritefmt nformat(#0.000);

putexcel save;
};
* end of making Appendix Table 7;



* beginning of making Appendix Table 8;
if(`makeapptable8'==1) 
{;
* Appendix Table 8 Regressions;

openregulardataset `sample' `tsample';


* dd spec post only.  No pre-test - Pooled Version;
* Copy table template;
copy "${maindir}/table templates/appendix/Appendix_Table8_template.xlsx" "${maindir}/results/appendix_table8_`sample'_`tsample'.xlsx", replace public;
putexcel set "${maindir}/results/appendix_table8_`sample'_`tsample'.xlsx", modify sheet("template");

clear matrix;

foreach var in `apptable8vars' 
{;
	* Aid ;
	preserve;
	
	keep if ia == 1;
	xtreg `var' post_wgtintens post weighted_intensity 
	_Iqdate*, robust i(ncid) fe cluster(id);

	mat cola = [_b[post_wgtintens] \ _se[post_wgtintens]];
	sum `var' if e(sample)==1 & qsince==-1 & ia==1;
	mat cola = [cola \ `r(mean)' \ e(blank) \ e(r2_o) \ e(N)];
	mat panela = [nullmat(panela), cola];
	
	restore;

	* No Aid ;
	preserve;
	
	keep if ia == 0;
	xtreg `var' post_wgtintens post weighted_intensity 
	_Iqdate*, robust i(ncid) fe cluster(id);

	mat cola = [_b[post_wgtintens] \ _se[post_wgtintens]];
	sum `var' if e(sample)==1 & qsince==-1 & ia==0;
	mat cola = [cola \ `r(mean)' \ e(blank) \ e(r2_o) \ e(N)];
	mat panela = [nullmat(panela), cola];

	restore;

	* Aid ;
	preserve;
	
	keep if ia == 1;
	xtreg `var' post_ef_group1 post_ef_group2 post_ef_group3 post ef_group1 ef_group2 ef_group3 
	 _Iqdate*, robust i(ncid) fe cluster(id);
	 
	mat colb = [_b[post_ef_group1] \ _se[post_ef_group1]];	
	sum `var' if e(sample)==1 & ef_group1==1 & qsince==-1 & ia==1;
	mat colb = [colb \ `r(mean)' \ e(blank)];
	mat colb = [colb \ _b[post_ef_group2] \ _se[post_ef_group2]];	
	sum `var' if e(sample)==1 & ef_group2==1 & qsince==-1 & ia==1;
	mat colb = [colb \ `r(mean)' \ e(blank)];
	mat colb = [colb \ _b[post_ef_group3] \ _se[post_ef_group3]];
	sum `var' if e(sample)==1 & ef_group3==1 & qsince==-1 & ia==1;
	mat colb = [colb \ `r(mean)' \ e(blank) \ e(r2_o) \ e(N)];
	mat panelb = [nullmat(panelb), colb];

	restore;
	
	* No Aid ;
	preserve;
	
	keep if ia == 0;
	xtreg `var' post_ef_group1 post_ef_group2 post_ef_group3 post ef_group1 ef_group2 ef_group3 
	 _Iqdate*, robust i(ncid) fe cluster(id);

	mat colb = [_b[post_ef_group1] \ _se[post_ef_group1]];	
	sum `var' if e(sample)==1 & ef_group1==1 & qsince==-1 & ia==0;
	mat colb = [colb \ `r(mean)' \ e(blank)];
	mat colb = [colb \ _b[post_ef_group2] \ _se[post_ef_group2]];	
	sum `var' if e(sample)==1 & ef_group2==1 & qsince==-1 & ia==0;
	mat colb = [colb \ `r(mean)' \ e(blank)];
	mat colb = [colb \ _b[post_ef_group3] \ _se[post_ef_group3]];
	sum `var' if e(sample)==1 & ef_group3==1 & qsince==-1 & ia==0;
	mat colb = [colb \ `r(mean)' \ e(blank) \ e(r2_o) \ e(N)];
	mat panelb = [nullmat(panelb), colb];

	restore;
};
putexcel B7 = matrix(panela);
putexcel B15 = matrix(panelb);

* format the R^2;
putexcel B11:I11, overwritefmt nformat(#0.000);
putexcel B27:I27, overwritefmt nformat(#0.000);

putexcel save;
};
* end of making Appendix Table 8;



* beginning of making Appendix Table11;
if(`makeapptable11'==1) 
{;
* Appendix Table 11 Regressions;

openregulardataset `sample' `tsample';


* ddd spec post only.  No pre-test - Pooled Version;
* Copy table template;
copy "${maindir}/table templates/appendix/Appendix_Table11_template.xlsx" "${maindir}/results/appendix_table11_`sample'_`tsample'.xlsx", replace public;
putexcel set "${maindir}/results/appendix_table11_`sample'_`tsample'.xlsx", modify sheet("template");

clear matrix;

foreach var in `apptable11vars' 
{; 
	xtreg `var' post_ia_wgtintens post_ia post_wgtintens post weighted_intensity ia
	_Iqdate*, robust i(ncid) fe cluster(id);
	mat cola = [_b[post_ia_wgtintens] \ _se[post_ia_wgtintens]];
	sum `var' if e(sample)==1 & qsince==-1 & treated==1 & ia==1;
	mat cola = [cola \ `r(mean)' \ e(blank) \ e(r2_o) \ e(N)];
	mat panela = [nullmat(panela), cola];

	xtreg `var' post_ia_ef_group1 post_ia_ef_group2 post_ia_ef_group3 post_ia post_ef_group1 post_ef_group2 post_ef_group3 post ef_group1 ef_group2 ef_group3 ia
	 _Iqdate*, robust i(ncid) fe cluster(id);
	mat colb = [_b[post_ia_ef_group1] \ _se[post_ia_ef_group1]];
	sum `var' if e(sample)==1 & ef_group1==1 & qsince==-1 & treated==1 & ia==1;
	mat colb = [colb \ `r(mean)' \ e(blank)];

	mat colb = [colb \ _b[post_ia_ef_group2] \ _se[post_ia_ef_group2]];
	sum `var' if e(sample)==1 & ef_group2==1 & qsince==-1 & treated==1 & ia==1;
	mat colb = [colb \ `r(mean)' \ e(blank)];

	mat colb = [colb \ _b[post_ia_ef_group3] \ _se[post_ia_ef_group3]];
	sum `var' if e(sample)==1 & ef_group3==1 & qsince==-1 & treated==1 & ia==1;
	mat colb = [colb \ `r(mean)' \ e(blank)];
	mat colb = [colb \ e(r2_o) \ e(N)];
	mat panelb = [nullmat(panelb), colb];
};
putexcel B8 = matrix(panela);
putexcel B17 = matrix(panelb);

* format the R^2;
putexcel B12:G12, overwritefmt nformat(#0.000);
putexcel B29:G29, overwritefmt nformat(#0.000);

putexcel save;
};
* end of making Appendix Table 11;



* beginning of making Appendix Table 12;
if(`makeapptable12'==1) 
{;
* Appendix Table 12 Regressions;

openregulardataset `sample' `tsample';


* ddd spec post only.  No pre-test - Pooled Version;
* Copy table template;
copy "${maindir}/table templates/appendix/Appendix_Table12_template.xlsx" "${maindir}/results/appendix_table12_`sample'_`tsample'.xlsx", replace public;
putexcel set "${maindir}/results/appendix_table12_`sample'_`tsample'.xlsx", modify sheet("template");

clear matrix;

foreach var in `apptable12vars' 
{; 
	xtreg `var'_lac post_ia_wgtintens post_ia post_wgtintens post weighted_intensity ia
	_Iqdate*, robust i(ncid) fe cluster(id);
	mat cola1 = [_b[post_ia_wgtintens] \ _se[post_ia_wgtintens]];
	sum `var'_lac if e(sample)==1 & qsince==-1 & treated==1 & ia==1;
	mat cola1 = [cola1 \ `r(mean)' \ e(N)];
	mat panela1 = [nullmat(panela1), cola1];

	xtreg `var'_hac post_ia_wgtintens post_ia post_wgtintens post weighted_intensity ia
	_Iqdate*, robust i(ncid) fe cluster(id);
	mat cola2 = [_b[post_ia_wgtintens] \ _se[post_ia_wgtintens]];
	sum `var'_hac if e(sample)==1 & qsince==-1 & treated==1 & ia==1;
	mat cola2 = [cola2 \ `r(mean)' \ e(N)];
	mat panela2 = [nullmat(panela2), cola2];

	xtreg `var'_lrs post_ia_wgtintens post_ia post_wgtintens post weighted_intensity ia
	_Iqdate*, robust i(ncid) fe cluster(id);
	mat colb1 = [_b[post_ia_wgtintens] \ _se[post_ia_wgtintens]];
	sum `var'_lrs if e(sample)==1 & qsince==-1 & treated==1 & ia==1;
	mat colb1 = [colb1 \ `r(mean)' \ e(N)];
	mat panelb1 = [nullmat(panelb1), colb1];

	xtreg `var'_hrs post_ia_wgtintens post_ia post_wgtintens post weighted_intensity ia
	_Iqdate*, robust i(ncid) fe cluster(id);
	mat colb2 = [_b[post_ia_wgtintens] \ _se[post_ia_wgtintens]];
	sum `var'_hrs if e(sample)==1 & qsince==-1 & treated==1 & ia==1;
	mat colb2 = [colb2 \ `r(mean)' \ e(N)];
	mat panelb2 = [nullmat(panelb2), colb2];

};
putexcel B9 = matrix(panela1);
putexcel B15 = matrix(panela2);

putexcel B23 = matrix(panelb1);
putexcel B29 = matrix(panelb2);

putexcel save;
};
* end of making Appendix Table 12;



local sample = "near_05_15";
local tsample = "zscoreccp8";


* beginning of making Appendix Table 13;
if(`makeapptable13'==1) 
{;
* Appendix Table 13 Means;

openregulardataset `sample' `tsample';


* Copy table template;
copy "${maindir}/table templates/appendix/Appendix_Table3_template.xlsx" "${maindir}/results/appendix_table13_`sample'_`tsample'.xlsx", replace public;
putexcel set "${maindir}/results/appendix_table13_`sample'_`tsample'.xlsx", modify sheet("template");

clear matrix;

local counter = 1;
foreach var in `apptable3varspanela' 
{;
if(`counter' == 6) {;
	mat row = [e(blank), e(blank), e(blank), e(blank), e(blank), e(blank), e(blank), e(blank)];
	mat panela = [nullmat(panela) \ row];
};
	sum `var' if qsince==-1 & treated==1;
	mat row = [`r(mean)'];
	sum `var' if qsince==-1 & treated==0;
	mat row = [row, `r(mean)'];
	sum `var' if qsince==-1 & ia==1;
	mat row = [row, `r(mean)'];
	sum `var' if qsince==-1 & ia==1 & treated==1;
	mat row = [row, `r(mean)'];
	sum `var' if qsince==-1 & ia==1 & treated==0;
	mat row = [row, `r(mean)'];
	sum `var' if qsince==-1 & ia==0;
	mat row = [row, `r(mean)'];
	sum `var' if qsince==-1 & ia==0 & treated==1;
	mat row = [row, `r(mean)'];
	sum `var' if qsince==-1 & ia==0 & treated==0;
	mat row = [row, `r(mean)'];

	mat panela = [nullmat(panela) \ row];
	local counter = `counter' + 1;
};

foreach var in `apptable3varspanelb' 
{;
	sum `var' if qsince==-1 & treated==1;
	mat row = [`r(mean)'];
	sum `var' if qsince==-1 & treated==0;
	mat row = [row, `r(mean)'];
	sum `var' if qsince==-1 & ia==1;
	mat row = [row, `r(mean)'];
	sum `var' if qsince==-1 & ia==1 & treated==1;
	mat row = [row, `r(mean)'];
	sum `var' if qsince==-1 & ia==1 & treated==0;
	mat row = [row, `r(mean)'];
	sum `var' if qsince==-1 & ia==0;
	mat row = [row, `r(mean)'];
	sum `var' if qsince==-1 & ia==0 & treated==1;
	mat row = [row, `r(mean)'];
	sum `var' if qsince==-1 & ia==0 & treated==0;
	mat row = [row, `r(mean)'];
				
	mat panelb = [nullmat(panelb) \ row];
};

* keep only one observation per person;
keep if qsince==-1;

count;
count if cid~=cid[_n-1];

sort cid;
count if cid~=cid[_n-1] & treated==1;
mat rowpeople = [`r(N)'];

count if cid~=cid[_n-1] & treated==0;
mat rowpeople = [rowpeople, `r(N)'];

count if cid~=cid[_n-1] & ia==1;
mat rowpeople = [rowpeople, `r(N)'];

count if cid~=cid[_n-1] & treated==1 & ia==1;
mat rowpeople = [rowpeople, `r(N)'];

count if cid~=cid[_n-1] & treated==0 & ia==1;
mat rowpeople = [rowpeople, `r(N)'];

count if cid~=cid[_n-1] & ia==0;
mat rowpeople = [rowpeople, `r(N)'];

count if cid~=cid[_n-1] & treated==1 & ia==0;
mat rowpeople = [rowpeople, `r(N)'];

count if cid~=cid[_n-1] & treated==0 & ia==0;
mat rowpeople = [rowpeople, `r(N)'];


sort block_str cid;
drop if block_str==block_str[_n-1];

count if treated==1;
mat rowblocks = [`r(N)'];

count if treated==0;
mat rowblocks = [rowblocks, `r(N)'];

count if ia==1;
mat rowblocks = [rowblocks, `r(N)'];

count if treated==1 & ia==1;
mat rowblocks = [rowblocks, `r(N)'];

count if treated==0 & ia==1;
mat rowblocks = [rowblocks, `r(N)'];

count if ia==0;
mat rowblocks = [rowblocks, `r(N)'];

count if treated==1 & ia==0;
mat rowblocks = [rowblocks, `r(N)'];

count if treated==0 & ia==0;
mat rowblocks = [rowblocks, `r(N)'];

mat panelc = [rowpeople \ rowblocks]; 


putexcel B8 = matrix(panela);
putexcel B20 = matrix(panelb);
putexcel B31 = matrix(panelc);

putexcel save;

};
* end of making Appendix Table 13;





* beginning of making Table14;
if(`makeapptable14'==1) 
{;
* Appendix Table 14 Regressions;

local sample = "near_05_15";
local tsample = "shawn";

disp("Sample: `sample'");
disp("T-Sample: `tsample'");

openregulardataset `sample' `tsample';


* dd spec post only.  No pre-test - Pooled Version;
* Copy table template;
copy "${maindir}/table templates/appendix/Appendix_Table14_template.xlsx" "${maindir}/results/appendix_table14.xlsx", replace public;
putexcel set "${maindir}/results/appendix_table14.xlsx", modify sheet("template");

* We are running bootstrapped standard erros so they will show up in the log file, but;
* we are not saving them to the table since at the precision of our formatting they;
* are exactly the same as the non-bootstrapped standard errors;

clear matrix;

foreach var in `table1vars' 
{;
	xtreg `var' post_wgtintens post weighted_intensity 
	_Iqdate*, robust i(ncid) fe cluster(id);
	mat cola = [_b[post_wgtintens] \ _se[post_wgtintens]];
	boottest post_wgtintens, reps(9999) gridpoints(10) bootcluster(id);
	* calculate bootstrapped standard error equivalent point estimate divided by t-stat;
	local bootse = _b[post_wgtintens] / `r(t)';
	sum `var' if e(sample)==1 & qsince==-1 & treated==1;
	mat cola = [cola \ `r(mean)' \ e(r2_o) \ e(N)];
	mat panela = [nullmat(panela), cola];

};

putexcel B8 = matrix(panela);

* format the R^2;
putexcel B11:G11, overwritefmt nformat(#0.000);

putexcel save;

openmigrationdataset `sample' `tsample';


* dd spec post only.  No pre-test - Pooled Version;
putexcel set "${maindir}/results/appendix_table14.xlsx", modify sheet("template");

clear matrix;

foreach var in `table1migvars' 
{;
	xtreg `var' post_wgtintens post weighted_intensity 
	_Iqdate*, robust i(ncid) fe cluster(id);
	mat cola = [_b[post_wgtintens] \ _se[post_wgtintens]];
	boottest post_wgtintens, reps(9999) gridpoints(10) bootcluster(id);
	* calculate bootstrapped standard error equivalent point estimate divided by t-stat;
	local bootse = _b[post_wgtintens] / `r(t)';
	sum `var' if e(sample)==1 & qsince==-1 & treated==1;
	mat cola = [cola \ `r(mean)' \ e(r2_o) \ e(N)];
	mat panela = [nullmat(panela), cola];

};

putexcel H8 = matrix(panela);

* format the R^2;
putexcel H12:I12, overwritefmt nformat(#0.000);

putexcel save;



local sample = "near_1_2";
local tsample = "shawn";

disp("Sample: `sample'");
disp("T-Sample: `tsample'");

openregulardataset `sample' `tsample';


* dd spec post only.  No pre-test - Pooled Version;
putexcel set "${maindir}/results/appendix_table14.xlsx", modify sheet("template");

clear matrix;

foreach var in `table1vars' 
{;
	xtreg `var' post_wgtintens post weighted_intensity 
	_Iqdate*, robust i(ncid) fe cluster(id);
	mat cola = [_b[post_wgtintens] \ _se[post_wgtintens]];
	sum `var' if e(sample)==1 & qsince==-1 & treated==1;
	mat cola = [cola \ `r(mean)' \ e(r2_o) \ e(N)];
	mat panela = [nullmat(panela), cola];

};

putexcel B14 = matrix(panela);

* format the R^2;
putexcel B17:G17, overwritefmt nformat(#0.000);

putexcel save;

openmigrationdataset `sample' `tsample';


* dd spec post only.  No pre-test - Pooled Version;
putexcel set "${maindir}/results/appendix_table14.xlsx", modify sheet("template");

clear matrix;

foreach var in `table1migvars' 
{;
	xtreg `var' post_wgtintens post weighted_intensity 
	_Iqdate*, robust i(ncid) fe cluster(id);
	mat cola = [_b[post_wgtintens] \ _se[post_wgtintens]];
	sum `var' if e(sample)==1 & qsince==-1 & treated==1;
	mat cola = [cola \ `r(mean)' \ e(r2_o) \ e(N)];
	mat panela = [nullmat(panela), cola];

};

putexcel H14 = matrix(panela);

* format the R^2;
putexcel H17:I17, overwritefmt nformat(#0.000);

putexcel save;


* Appendix Table 14 Stacked DiD Regressions;

local sample = "near_05_15";
local tsample = "shawn";

disp("Sample: `sample'");
disp("T-Sample: `tsample'");

openregulardataset `sample' `tsample';


* stacked dd spec post only.  No pre-test - Pooled Version;
putexcel set "${maindir}/results/appendix_table14.xlsx", modify sheet("template");

clear matrix;

foreach var in `table1vars' 
{;
	xtreg `var' post_wgtintens post weighted_intensity 
	id#qdate, robust i(ncid) fe cluster(id);
	mat cola = [_b[post_wgtintens] \ _se[post_wgtintens]];
	sum `var' if e(sample)==1 & qsince==-1 & treated==1;
	mat cola = [cola \ `r(mean)' \ e(r2_o) \ e(N)];
	mat panela = [nullmat(panela), cola];

};

putexcel B20 = matrix(panela);

* format the R^2;
putexcel B23:G23, overwritefmt nformat(#0.000);

putexcel save;


* beginning of making stacked Migration DiD Table1;
* Table 1 Stacked MigrationDiD Regressions;

openmigrationdataset `sample' `tsample';


* stacked dd spec post only.  No pre-test - Pooled Version;
* Copy table template;
putexcel set "${maindir}/results/appendix_table14.xlsx", modify sheet("template");

clear matrix;

foreach var in `table1migvars' 
{;
	xtreg `var' post_wgtintens post weighted_intensity 
	id#qdate, robust i(ncid) fe cluster(id);
	mat cola = [_b[post_wgtintens] \ _se[post_wgtintens]];
	sum `var' if e(sample)==1 & qsince==-1 & treated==1;
	mat cola = [cola \ `r(mean)' \ e(r2_o) \ e(N)];
	mat panela = [nullmat(panela), cola];
};

putexcel H20 = matrix(panela);

* format the R^2;
putexcel H23:I23, overwritefmt nformat(#0.000);

putexcel save;


local sample = "near_05_15";
local tsample = "zscoreccp8";

disp("Sample: `sample'");
disp("T-Sample: `tsample'");

openregulardataset `sample' `tsample';


* dd spec post only.  No pre-test - Pooled Version;
* Copy table template;
putexcel set "${maindir}/results/appendix_table14.xlsx", modify sheet("template");

clear matrix;

foreach var in `table1vars' 
{;
	xtreg `var' post_wgtintens post weighted_intensity 
	_Iqdate*, robust i(ncid) fe cluster(id);
	mat cola = [_b[post_wgtintens] \ _se[post_wgtintens]];
	sum `var' if e(sample)==1 & qsince==-1 & treated==1;
	mat cola = [cola \ `r(mean)' \ e(r2_o) \ e(N)];
	mat panela = [nullmat(panela), cola];

};

putexcel B26 = matrix(panela);

* format the R^2;
putexcel B29:G29, overwritefmt nformat(#0.000);

putexcel save;


openmigrationdataset `sample' `tsample';

* dd spec post only.  No pre-test - Pooled Version;
putexcel set "${maindir}/results/appendix_table14.xlsx", modify sheet("template");

clear matrix;

foreach var in `table1migvars' 
{;
	xtreg `var' post_wgtintens post weighted_intensity 
	_Iqdate*, robust i(ncid) fe cluster(id);
	mat cola = [_b[post_wgtintens] \ _se[post_wgtintens]];
	sum `var' if e(sample)==1 & qsince==-1 & treated==1;
	mat cola = [cola \ `r(mean)' \ e(r2_o) \ e(N)];
	mat panela = [nullmat(panela), cola];

};

putexcel H26 = matrix(panela);

* format the R^2;
putexcel H29:I29, overwritefmt nformat(#0.000);

putexcel save;


local sample = "near_05_15";
local tsample = "full";

disp("Sample: `sample'");
disp("T-Sample: `tsample'");

openregulardataset `sample' `tsample';


* dd spec post only.  No pre-test - Pooled Version;
* Copy table template;
putexcel set "${maindir}/results/appendix_table14.xlsx", modify sheet("template");

clear matrix;

foreach var in `table1vars' 
{;
	xtreg `var' post_wgtintens post weighted_intensity 
	_Iqdate*, robust i(ncid) fe cluster(id);
	mat cola = [_b[post_wgtintens] \ _se[post_wgtintens]];
	sum `var' if e(sample)==1 & qsince==-1 & treated==1;
	mat cola = [cola \ `r(mean)' \ e(r2_o) \ e(N)];
	mat panela = [nullmat(panela), cola];

};

putexcel B32 = matrix(panela);

* format the R^2;
putexcel B35:G35, overwritefmt nformat(#0.000);

putexcel save;


openmigrationdataset `sample' `tsample';

* dd spec post only.  No pre-test - Pooled Version;
putexcel set "${maindir}/results/appendix_table14.xlsx", modify sheet("template");

clear matrix;

foreach var in `table1migvars' 
{;
	xtreg `var' post_wgtintens post weighted_intensity 
	_Iqdate*, robust i(ncid) fe cluster(id);
	mat cola = [_b[post_wgtintens] \ _se[post_wgtintens]];
	sum `var' if e(sample)==1 & qsince==-1 & treated==1;
	mat cola = [cola \ `r(mean)' \ e(r2_o) \ e(N)];
	mat panela = [nullmat(panela), cola];

};

putexcel H32 = matrix(panela);

* format the R^2;
putexcel H35:I35, overwritefmt nformat(#0.000);

putexcel save;

};
* end of making Table 14;






* beginning of making Table15;
if(`makeapptable15'==1) 
{;
* Appendix Table 15 Regressions;

local sample = "near_05_15";
local tsample = "shawn";

disp("Sample: `sample'");
disp("T-Sample: `tsample'");

openregulardataset `sample' `tsample';


* dd spec post only.  No pre-test - Pooled Version;
* Copy table template;
copy "${maindir}/table templates/appendix/Appendix_Table15_template.xlsx" "${maindir}/results/appendix_table15.xlsx", replace public;
putexcel set "${maindir}/results/appendix_table15.xlsx", modify sheet("template");

clear matrix;

foreach var in `table1vars' 
{;
	xtreg `var' post_ia_wgtintens post_ia post_wgtintens post weighted_intensity ia
	_Iqdate*, robust i(ncid) fe cluster(id);
	mat cola = [_b[post_ia_wgtintens] \ _se[post_ia_wgtintens]];
	boottest post_ia_wgtintens, reps(9999) gridpoints(10) bootcluster(id);
	* calculate bootstrapped standard error equivalent point estimate divided by t-stat;
	local bootse = _b[post_ia_wgtintens] / `r(t)';
	sum `var' if e(sample)==1 & qsince==-1 & treated==1 & ia==1;
	mat cola = [cola \ `r(mean)' \ e(r2_o) \ e(N)];
	mat panela = [nullmat(panela), cola];
};

putexcel B8 = matrix(panela);

* format the R^2;
putexcel B11:G11, overwritefmt nformat(#0.000);

putexcel save;

openmigrationdataset `sample' `tsample';


* dd spec post only.  No pre-test - Pooled Version;
putexcel set "${maindir}/results/appendix_table15.xlsx", modify sheet("template");

clear matrix;

foreach var in `table1migvars' 
{;
	xtreg `var' post_ia_wgtintens post_ia post_wgtintens post weighted_intensity ia
	_Iqdate*, robust i(ncid) fe cluster(id);
	mat cola = [_b[post_ia_wgtintens] \ _se[post_ia_wgtintens]];
	boottest post_ia_wgtintens, reps(9999) gridpoints(10) bootcluster(id);
	* calculate bootstrapped standard error equivalent point estimate divided by t-stat;
	local bootse = _b[post_ia_wgtintens] / `r(t)';
	sum `var' if e(sample)==1 & qsince==-1 & treated==1 & ia==1;
	mat cola = [cola \ `r(mean)' \ e(r2_o) \ e(N)];
	mat panela = [nullmat(panela), cola];
};

putexcel H8 = matrix(panela);

* format the R^2;
putexcel H11:I11, overwritefmt nformat(#0.000);

putexcel save;



local sample = "near_1_2";
local tsample = "shawn";

disp("Sample: `sample'");
disp("T-Sample: `tsample'");

openregulardataset `sample' `tsample';


* dd spec post only.  No pre-test - Pooled Version;
putexcel set "${maindir}/results/appendix_table15.xlsx", modify sheet("template");

clear matrix;

foreach var in `table1vars' 
{;
	xtreg `var' post_ia_wgtintens post_ia post_wgtintens post weighted_intensity ia
	_Iqdate*, robust i(ncid) fe cluster(id);
	mat cola = [_b[post_ia_wgtintens] \ _se[post_ia_wgtintens]];
	sum `var' if e(sample)==1 & qsince==-1 & treated==1 & ia==1;
	mat cola = [cola \ `r(mean)' \ e(r2_o) \ e(N)];
	mat panela = [nullmat(panela), cola];
};

putexcel B14 = matrix(panela);

* format the R^2;
putexcel B17:G17, overwritefmt nformat(#0.000);

putexcel save;

openmigrationdataset `sample' `tsample';


* dd spec post only.  No pre-test - Pooled Version;
putexcel set "${maindir}/results/appendix_table15.xlsx", modify sheet("template");

clear matrix;

foreach var in `table1migvars' 
{;
	xtreg `var' post_ia_wgtintens post_ia post_wgtintens post weighted_intensity ia
	_Iqdate*, robust i(ncid) fe cluster(id);
	mat cola = [_b[post_ia_wgtintens] \ _se[post_ia_wgtintens]];
	sum `var' if e(sample)==1 & qsince==-1 & treated==1 & ia==1;
	mat cola = [cola \ `r(mean)' \ e(r2_o) \ e(N)];
	mat panela = [nullmat(panela), cola];
};

putexcel H14 = matrix(panela);

* format the R^2;
putexcel H17:I17, overwritefmt nformat(#0.000);

putexcel save;


* Appendix Table 15 Stacked DiD Regressions;

local sample = "near_05_15";
local tsample = "shawn";

disp("Sample: `sample'");
disp("T-Sample: `tsample'");

openregulardataset `sample' `tsample';


* stacked dd spec post only.  No pre-test - Pooled Version;
putexcel set "${maindir}/results/appendix_table15.xlsx", modify sheet("template");

clear matrix;

foreach var in `table1vars' 
{;
	xtreg `var' post_ia_wgtintens post_ia post_wgtintens post weighted_intensity ia
	id#qdate, robust i(ncid) fe cluster(id);
	mat cola = [_b[post_ia_wgtintens] \ _se[post_ia_wgtintens]];
	sum `var' if e(sample)==1 & qsince==-1 & treated==1 & ia==1;
	mat cola = [cola \ `r(mean)' \ e(r2_o) \ e(N)];
	mat panela = [nullmat(panela), cola];
};

putexcel B20 = matrix(panela);

* format the R^2;
putexcel B23:G23, overwritefmt nformat(#0.000);

putexcel save;


* beginning of making stacked Migration DiD Table1;
* Table 1 Stacked MigrationDiD Regressions;

openmigrationdataset `sample' `tsample';


* stacked dd spec post only.  No pre-test - Pooled Version;
* Copy table template;
putexcel set "${maindir}/results/appendix_table15.xlsx", modify sheet("template");

clear matrix;

foreach var in `table1migvars' 
{;
	xtreg `var' post_ia_wgtintens post_ia post_wgtintens post weighted_intensity ia
	id#qdate, robust i(ncid) fe cluster(id);
	mat cola = [_b[post_ia_wgtintens] \ _se[post_ia_wgtintens]];
	sum `var' if e(sample)==1 & qsince==-1 & treated==1 & ia==1;
	mat cola = [cola \ `r(mean)' \ e(r2_o) \ e(N)];
	mat panela = [nullmat(panela), cola];
};

putexcel H20 = matrix(panela);

* format the R^2;
putexcel H23:I23, overwritefmt nformat(#0.000);

putexcel save;


local sample = "near_05_15";
local tsample = "zscoreccp8";

disp("Sample: `sample'");
disp("T-Sample: `tsample'");

openregulardataset `sample' `tsample';


* dd spec post only.  No pre-test - Pooled Version;
* Copy table template;
putexcel set "${maindir}/results/appendix_table15.xlsx", modify sheet("template");

clear matrix;

foreach var in `table1vars' 
{;
	xtreg `var' post_ia_wgtintens post_ia post_wgtintens post weighted_intensity ia
	_Iqdate*, robust i(ncid) fe cluster(id);
	mat cola = [_b[post_ia_wgtintens] \ _se[post_ia_wgtintens]];
	sum `var' if e(sample)==1 & qsince==-1 & treated==1 & ia==1;
	mat cola = [cola \ `r(mean)' \ e(r2_o) \ e(N)];
	mat panela = [nullmat(panela), cola];
};

putexcel B26 = matrix(panela);

* format the R^2;
putexcel B29:G29, overwritefmt nformat(#0.000);

putexcel save;


openmigrationdataset `sample' `tsample';

* dd spec post only.  No pre-test - Pooled Version;
putexcel set "${maindir}/results/appendix_table15.xlsx", modify sheet("template");

clear matrix;

foreach var in `table1migvars' 
{;
	xtreg `var' post_ia_wgtintens post_ia post_wgtintens post weighted_intensity ia
	_Iqdate*, robust i(ncid) fe cluster(id);
	mat cola = [_b[post_ia_wgtintens] \ _se[post_ia_wgtintens]];
	sum `var' if e(sample)==1 & qsince==-1 & treated==1 & ia==1;
	mat cola = [cola \ `r(mean)' \ e(r2_o) \ e(N)];
	mat panela = [nullmat(panela), cola];
};

putexcel H26 = matrix(panela);

* format the R^2;
putexcel H29:I29, overwritefmt nformat(#0.000);

putexcel save;


local sample = "near_05_15";
local tsample = "full";

disp("Sample: `sample'");
disp("T-Sample: `tsample'");

openregulardataset `sample' `tsample';


* dd spec post only.  No pre-test - Pooled Version;
* Copy table template;
putexcel set "${maindir}/results/appendix_table15.xlsx", modify sheet("template");

clear matrix;

foreach var in `table1vars' 
{;
	xtreg `var' post_ia_wgtintens post_ia post_wgtintens post weighted_intensity ia
	_Iqdate*, robust i(ncid) fe cluster(id);
	mat cola = [_b[post_ia_wgtintens] \ _se[post_ia_wgtintens]];
	sum `var' if e(sample)==1 & qsince==-1 & treated==1 & ia==1;
	mat cola = [cola \ `r(mean)' \ e(r2_o) \ e(N)];
	mat panela = [nullmat(panela), cola];
};

putexcel B32 = matrix(panela);

* format the R^2;
putexcel B35:G35, overwritefmt nformat(#0.000);

putexcel save;


openmigrationdataset `sample' `tsample';

* dd spec post only.  No pre-test - Pooled Version;
putexcel set "${maindir}/results/appendix_table15.xlsx", modify sheet("template");

clear matrix;

foreach var in `table1migvars' 
{;
	xtreg `var' post_ia_wgtintens post_ia post_wgtintens post weighted_intensity ia
	_Iqdate*, robust i(ncid) fe cluster(id);
	mat cola = [_b[post_ia_wgtintens] \ _se[post_ia_wgtintens]];
	sum `var' if e(sample)==1 & qsince==-1 & treated==1 & ia==1;
	mat cola = [cola \ `r(mean)' \ e(r2_o) \ e(N)];
	mat panela = [nullmat(panela), cola];
};

putexcel H32 = matrix(panela);

* format the R^2;
putexcel H35:I35, overwritefmt nformat(#0.000);

putexcel save;

};
* end of making Table 15;






local tt = 1;
local sample = "near_05_15";
local tsample = "shawn";


* Appendix Figure 8;
* beginning of making annual DD BJS eventstudy;
if(`makeannualddbjseventstudy'==1) 
{;
* BJS eventstudy Regressions and Figures;

openregulardataset `sample' `tsample';


* annual dd spec eventstudy figure;
clear matrix;

* collapse to annual level in event time except quarter of tornado
tab qsince;
replace qsince = qsince / 4;
tab qsince;
replace qsince = ceil(qsince) if qsince > 0;
tab qsince;
replace qsince = floor(qsince) if qsince < 0;
tab qsince;

collapse (mean) qdate `bjsvars', by(ncid qsince treated ef_group1 ef_group2 ef_group3 id);


replace qdate = round(qdate);

gen year = year(dofq(qdate)) if qsince == 0;
bys ncid (year): replace year = year[1];
replace year = year + qsince;

tab year;

* set up data for did_imputation;
gen Ei = year if qsince == 0;
bys ncid (Ei): replace Ei = Ei[1];

* setting the controls to not have any event time (otherwise they will be counted as treated);
replace Ei = . if treated == 0;


foreach var in `bjsvars'
{;
	* Event study dd spec - Damage Heterogeneity Version - Annual Estimates;
		  if "`var'" == "bal_bcret_tot" {;
			local title "A: Credit Card Debt";
			local ytitle "";
			local xtitle "Years Since Tornado";
			};
		  else if "`var'" == "bal_home_tot_cond" {;
			local title "B: Home Debt (Cond'l)";
			local ytitle "";
			local xtitle "Years Since Tornado";
			};
		  else if "`var'" == "bal_auto_tot" {;
			local title "C: Auto Debt";
			local ytitle "";
			local xtitle "Years Since Tornado";
			};	
		  else if "`var'" == "bal_other2_tot" {;
			local title "D: Other Debt";
			local ytitle "";
			local xtitle "Years Since Tornado";
			};			
		  else if "`var'" == "riskscore" {;
			local title "E: Equifax Risk Score";
			local ytitle "";
			local xtitle "Years Since Tornado";
			};	
		  else if "`var'" == "gt1_all_90" {;
			local title "F: 90 Day Delinquency";
			local ytitle "";
			local xtitle "Years Since Tornado";
			};	

	* Estimation with did_imputation of Borusyak et al. (2021);
	capture noisily {;
		did_imputation `var' ncid year Ei, autosample pretrend(3) horizons(0/3) maxit(100) cluster(id);
		test pre1 pre2;
		event_plot, default_look graph_opt(xtitle("`xtitle'") ytitle("`ytitle'") title("`title'") xlabel(-3(1)3) legend(off));
		graph save ${maindir}/figures/BJS_annual_`var'_alltreated_`sample'_`tsample'.gph, replace;
		graph export ${maindir}/figures/BJS_annual_`var'_alltreated_`sample'_`tsample'.pdf, replace;
	};


};

};
* end of making annual DD BJS eventstudy;

* Appendix Figure 8;
* beginning of making annual migration DD BJS eventstudy;
if(`makeannualddbjsmigeventstudy'==1) 
{;
* BJS eventstudy Regressions and Figures;

openmigrationdataset `sample' `tsample';


* annual dd spec eventstudy figure;
clear matrix;

* collapse to annual level in event time except quarter of tornado
tab qsince;
replace qsince = qsince / 4;
tab qsince;
replace qsince = ceil(qsince) if qsince > 0;
tab qsince;
replace qsince = floor(qsince) if qsince < 0;
tab qsince;

collapse (mean) qdate `bjsmigvars', by(ncid qsince treated ef_group1 ef_group2 ef_group3 id);

replace qdate = round(qdate);

gen year = year(dofq(qdate)) if qsince == 0;
bys ncid (year): replace year = year[1];
replace year = year + qsince;

tab year;

* set up data for did_imputation;
gen Ei = year if qsince == 0;
bys ncid (Ei): replace Ei = Ei[1];

* setting the controls to not have any event time (otherwise they will be counted as treated);
replace Ei = . if treated == 0;


foreach var in `bjsmigvars'
{;
	* Event study dd spec - Damage Heterogeneity Version - Quarterly Estimates;
		  if "`var'" == "moveblock" {;
			local title "G: Move from Block 1 Q";
			local ytitle "";
			local xtitle "Years Since Tornado";
			};
		  else if "`var'" == "movecounty" {;
			local title "G: Move from County 1 Quarter";
			local ytitle "";
			local xtitle "Years Since Tornado";
			};
		  else if "`var'" == "moveblock3yr" {;
			local title "H: Move from Block 3 Yrs";
			local ytitle "";
			local xtitle "Years Since Tornado";
			};	
		  else if "`var'" == "movecounty3yr" {;
			local title "H: Move from County 3 Years";
			local ytitle "";
			local xtitle "Years Since Tornado";
			};			

	* Estimation with did_imputation of Borusyak et al. (2021);
	capture noisily {;
		did_imputation `var' ncid year Ei, autosample pretrend(3) horizons(0/3) maxit(100) cluster(id);
		test pre1 pre2;
		event_plot, default_look graph_opt(xtitle("`xtitle'") ytitle("`ytitle'") title("`title'") xlabel(-3(1)3) legend(off));
		graph save ${maindir}/figures/BJS_annual_`var'_alltreated_`sample'_`tsample'.gph, replace;
		graph export ${maindir}/figures/BJS_annual_`var'_alltreated_`sample'_`tsample'.pdf, replace;
	};

};

};
* end of making annual migration DD BJS eventstudy;


* 2x4 Panel of BJS Plots;

if(`bjsplots2x4'==1) {;

	* create v1 version with q0 broken out on its own;
	capture noisily {;
		gr combine ${maindir}/figures/BJS_annual_bal_bcret_tot_alltreated_`sample'_`tsample'.gph	
		   ${maindir}/figures/BJS_annual_bal_home_tot_cond_alltreated_`sample'_`tsample'.gph
		   ${maindir}/figures/BJS_annual_bal_auto_tot_alltreated_`sample'_`tsample'.gph
		   ${maindir}/figures/BJS_annual_bal_other2_tot_alltreated_`sample'_`tsample'.gph 
		   ${maindir}/figures/BJS_annual_riskscore_alltreated_`sample'_`tsample'.gph
		   ${maindir}/figures/BJS_annual_gt1_all_90_alltreated_`sample'_`tsample'.gph
		   ${maindir}/figures/BJS_annual_moveblock_alltreated_`sample'_`tsample'.gph
		   ${maindir}/figures/BJS_annual_moveblock3yr_alltreated_`sample'_`tsample'.gph, 
		   col(4) scheme(s1mono) xsize(16) ysize(8);
		graph export "${maindir}/figures/appendix_figure8_bjs_alltreated_`sample'_`tsample'.pdf", as(pdf) replace;
	};
	* end of create v1 version;

};
* end of if;

* 2x4 Panel of BJS Plots;
* End of Appendix Figure 8;





log close;
